[Python-Dev] package imports, sys.path and os.chdir()
Christian Tismer
tismer at stackless.com
Thu Apr 26 23:30:40 CEST 2012
Howdy,
I have a small problem/observation with imports.
I have several packages to import, which works all fine, as long
as the packages are imported from directories found on the installed
site-packages, via .pth etc.
The only problem is the automatically prepended empty string in sys.path.
Depending from where I start my application, the values stored
in package.__file__ and package.__path__ are absolute or relative
paths.
So, if my pwd is the directory that contains my top-level modules,
even though sys.path contains correct absolute entries for that, in this
case the '' entry wins.
Assume this:
<- cwd is here
moda
modb
>>> import moda
Some code happens to chdir away, and later some code does
>>> from moda import modb
Since the __path__ entry is now a relative path, this second import fails.
Although it is no recommended practice to leave a changed chdir(), I
don't see why this is so. When a module is imported, would it not be
better to always make __file__ and __path__ absolute?
I see the module path, hidden by the '' entry not as a feature but
an undesired side-effect.
No big deal and easy to work around, I just would like to understand why.
cheers -- chris
--
Christian Tismer :^)<mailto:tismer at stackless.com>
tismerysoft GmbH : Have a break! Take a ride on Python's
Karl-Liebknecht-Str. 121 : *Starship* http://starship.python.net/
14482 Potsdam : PGP key -> http://pgp.uni-mainz.de
work +49 173 24 18 776 mobile +49 173 24 18 776 fax n.a.
PGP 0x57F3BF04 9064 F4E1 D754 C2FF 1619 305B C09C 5A3B 57F3 BF04
whom do you want to sponsor today? http://www.stackless.com/
More information about the Python-Dev
mailing list