Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to know who is importing me in python?

Tags:

python

import

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
...

#cmn.py
#Here, I want to know which file (a.py or b.py)
#is importing this one.
#Is it possible to do this?
...

All the files a.py, b.py and cmn.py are in the same directory.

Why do I want to do this?
In C/C++, they have include feature. What i want to do can illuminate by the C/C++ code.

//a.cpp
....
#define SOME_STUFF   ....
#include "cmn.h"

//b.cpp
...
#define SOME_STUFF   ....

#include "cmn.h"

//cmn.h
//Here, I'll define some functions/classes that will use the symbol define
//in the a.cpp or b.cpp
...
....code refer to the SOME_STUFF.....

In C/C++, we can use this method to reuse sourecode.

Now return to my python code.
When a.py import cmn.py, i hope to run cmn.py and the cmn.py will refer to the symbol defined in the a.py.
When b.py import cmn.py, i hope to run cmn.py and the cmn.py will refer to the symbol defined in the b.py.

like image 841
Yuncy Avatar asked Oct 24 '11 04:10

Yuncy


People also ask

How can I tell where Python is importing from?

Running "python -v"from the command line tells you what is being imported and from where. This is useful if you want to know the location of built in modules.

How do I know if a Python module is imported?

Use: if "sys" not in dir(): print("sys not imported!")

What is __ loader __ in Python?

__loader__ is an attribute that is set on an imported module by its loader. Accessing it should return the loader object itself. In Python versions before 3.3, __loader__ was not set by the built-in import machinery. Instead, this attribute was only available on modules that were imported using a custom loader.

What does __ import __ do in Python?

The __import__() in python module helps in getting the code present in another module by either importing the function or code or file using the import in python method. The import in python returns the object or module that we specified while using the import module.


2 Answers

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.

like image 196
Raymond Hettinger Avatar answered Sep 21 '22 06:09

Raymond Hettinger


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
like image 32
Dietrich Epp Avatar answered Sep 18 '22 06:09

Dietrich Epp