How do I find the name of the importer file in the imported file?
At the top level of c.py (i.e. outside of any function or class), you should get the information you need by running
import traceback
and then consider the result of traceback.extract_stack (). At the time the top-level code is executed, the module importer (and its importer, etc., recursively) are frozen.
source to share
This can be done by checking the stack:
#inside c.py:
import inspect
FRAME_FILENAME = 1
print "Imported from: ", inspect.getouterframes(inspect.currentframe())[-1][FRAME_FILENAME]
#or:
print "Imported from: ", inspect.stack()[-1][FRAME_FILENAME]
But checking the stack can be a mistake. Why do you need to know where the file is being imported from? Why not upload a file that imports ( a.py
and b.py
) name c.py
? (assuming you have control over a.py
and b.py
)
source to share