Finding path to __init__.py in package

Jeff Shannon jeff at ccvcorp.com
Thu Feb 14 19:35:56 EST 2002


Lulu of the Lotus-Eaters wrote:

> What I would like is a function that asks "what directory is this
> current __init__.py script running from?"

Every module has a __file__ attribute that is the (fully qualified) filename
that the module was loaded from -- so in your example __init__, it should be
"$PYTHONPATH/gnosis/xml/pickle/doc/__init__.py"  with, of course, $PYTHONPATH
properly expanded.  (Shouldn't that be sys.prefix, though, and not
$PYTHONPATH??)  I'm not positive whether the __file__ attribute is accessible
from *inside* the module, but it should be.  In which case, all you need to
do is os.path.dirname(__file__).

Jeff Shannon
Technician/Programmer
Credit International





More information about the Python-list mailing list