getting "this" module's path
Skip Montanaro
skip at pobox.com
Fri Jan 4 11:59:57 EST 2002
Clark> How do I get the path name for a module (in particular, the
Clark> module that is currently executing?).
Modules written in Python and dynamically linked extension modules all have
a __file__ attribute. Statically linked extension modules do not, however:
>>> import os
>>> os.__file__
'/usr/local/lib/python2.2/os.pyc'
>>> import math
>>> math.__file__
'/usr/local/lib/python2.2/lib-dynload/math.so'
>>> import sys
>>> sys.__file__
Traceback (most recent call last):
File "<stdin>", line 1, in ?
AttributeError: 'module' object has no attribute '__file__'
You probably don't need to know the path to statically linked modules, so
this is only a minor limitation (though for the sake of consistency it would
be nice if it didn't raise an exception, but returned an empty string or the
path to the executable).
--
Skip Montanaro (skip at pobox.com - http://www.mojam.com/)
More information about the Python-list
mailing list