currently executing python file

Here is a way to get the currently executing Python file:

print (lambda x:x).func_code.co_filename

Probably there is a simpler way, but I haven’t found it. The best alternative I found is to use the special attribute __file__, but it doesn’t work for SConscript files:

print (lambda x:x).func_code.co_filename
print __file__

results is

/home/olpa/p/.../SConscript.py
/usr/lib/python2.4/site-packages/SCons/Script/__init__.pyc

Leave a Reply