How can I find out what file is importing a particular file in python?
Consider the following example:
#a.py
import cmn
....
#b.py
import cmn
...
#
You'd need to install an import hook that tracks all imports. See PEP 302 and http://docs.python.org/dev/py3k/library/importlib.html. However, as the comments above point out, there is probably a better way to structure your code.
The namedtuple code in the collections module has an example of how (and when) to do this:
#cmn.py
import sys
print 'I am being imported by', sys._getframe(1).f_globals.get('__name__')
One limitation of this approach is that the outermost module is always named __main__
. If that is the case, the name of the outermost module can be determined from sys.argv[0]
.
A second limitation is that if the code using sys._getframe is in the module scope it is only executed on the first import of cmn.py. You'd need to call a function of some sort after imports if you want to monitor all imports of the module.
Well, this is a kind of bizarre thing to do. You haven't explained why you want to know what is importing your module, so I can't actually help you solve your problem. You also haven't explained how or when you want to know the importing module.
def who_imports(studied_module):
for loaded_module in sys.modules.values():
for module_attribute in dir(loaded_module):
if getattr(loaded_module, module_attribute) is studied_module:
yield loaded_module
This will give you an iterator over all the modules which use your module as a top-level object. It won't find modules that do from cmn import *
, and the list will change over time.
>>> import os
>>> for m in who_imports(os):
... print m.__name__
...
site
__main__
posixpath
genericpath
posixpath
linecache