Where is module being imported from?

Python

Python Problem Overview


Assuming I have two Python modules and path_b is in the import path:

# file: path_b/my_module.py
print "I was imported from ???"

#file: path_a/app.py
import my_module

Is it possible to see where the module is imported from? I want an output like "I was imported from path_a/app.py", if I start app.py (because I need the file name).

Edit: For better understanding; I could write:

# file: path_b/my_module.py
def foo(file):
    print "I was imported from %s" % file

#file: path_a/app.py
import my_module
my_module.foo(__file__)

So the output would be:

$> python path_app.py
I was imported from path_a/app.py

Python Solutions


Solution 1 - Python

Try this:

>>> import my_module
>>> my_module.__file__
'/Users/myUser/.virtualenvs/foobar/lib/python2.7/site-packages/my_module/__init__.pyc'

Edit

In that case write into the __init__.py file of your module:

print("%s: I was imported from %s" %(__name__, __file__))

Solution 2 - Python

There may be an easier way to do this, but this works:

import inspect

print inspect.getframeinfo(inspect.getouterframes(inspect.currentframe())[1][0])[0]

Note that the path will be printed relative to the current working directory if it's a parent directory of the script location.

Solution 3 - Python

Try my_module.__file__ to find out where it is from. If you get an AttributeError, it is probably not a Python source (.py) file.

Solution 4 - Python

Also, if you have a function/class f from a module m you can get the path of the module using the module inspect

import inspect
from m import f

print inspect.getmodule(f)

Solution 5 - Python

This is how I do it:

print(module_name.__path__)

Solution 6 - Python

I've written a simple script so I have the command pywhich that allows me to find where a Python module comes from. It doesn't work for some builtins like sys which doesn't have a \__file__ attribute.

This can be run from a Linux command line to find where a python script run in the current environment will get a module from, for example:

% pywhich os


#! /usr/bin/env python
def pywhich(module_name):
    module = __import__(module_name, globals(), locals(), [], 0)
    return module.__file__

if __name__ == "__main__":
    import sys
    print(pywhich(sys.argv[1]))

Solution 7 - Python

If you want to see where a module is stored, for example setuptools, type in shell:

$ python -c "import setuptools; print(setuptools.__file__)"

Solution 8 - Python

If you want to know where file x.py is being imported from - you could add raise RuntimeError() to the first line of x.py and inspect the stacktrace...

Solution 9 - Python

You don't need any additional packages. For python3:

python3 -m pip show <focal_package>

Solution 10 - Python

Other answers are OK, but if you want to tell it from inside the imported module then do

print "I was imported from %s" % __file__

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionsvenwltrView Question on Stackoverflow
Solution 1 - PythonJohannesView Answer on Stackoverflow
Solution 2 - PythonWoobleView Answer on Stackoverflow
Solution 3 - PythonNoctis SkytowerView Answer on Stackoverflow
Solution 4 - PythonFábio DinizView Answer on Stackoverflow
Solution 5 - PythonJoshua BurkhowView Answer on Stackoverflow
Solution 6 - PythonAdam AndersonView Answer on Stackoverflow
Solution 7 - PythonPhil L.View Answer on Stackoverflow
Solution 8 - PythontlonnyView Answer on Stackoverflow
Solution 9 - Pythonbshelt141View Answer on Stackoverflow
Solution 10 - PythonGuardView Answer on Stackoverflow