Where is module being imported from?

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__))

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.


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)

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.

Tags:

Python