[Tutor] how to determine where a module or function is called from

Kent Johnson kent37 at tds.net
Thu Feb 22 23:14:33 CET 2007


Lance Haig wrote:
> I am debugging a script that was written by someone else and I was 
> wondering if there is a way to determine where a function or module is 
> imported from

You can use the __file__ attribute of a module:
In [9]: csv.__file__
Out[9]: 
'/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/csv.pyc'

For a function use the __module__ attribute to get the module:
In [10]: csv.reader.__module__
Out[10]: '_csv'

This is a string so you have to do a little more work to get the file:

In [12]: import sys
In [13]: sys.modules[csv.reader.__module__].__file__
Out[13]: 
'/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/lib-dynload/_csv.so'

Kent


More information about the Tutor mailing list