Jakob Heitz
Jakob Heitz

Reputation: 33

How can you find where python imported a particular module from?

How can you find where python imported a particular module from?

Upvotes: 3

Views: 78

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1124738

Each module object has a __file__ attribute:

import module

print module.__file__

Some modules are part of the Python executable; these will not have the attribute set.

Demo:

>>> import urllib2
>>> urllib2.__file__
'/Users/mj/Development/Library/buildout.python/parts/opt/lib/python2.7/urllib2.pyc'
>>> import sys
>>> sys.__file__
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute '__file__'

You could also run Python in verbose mode, with the -v command line switch or the PYTHONVERBOSE environment variable; Python then prints out every imported file as it takes place.

Upvotes: 1

Related Questions