Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Dev
package imports, sys.path and os.chdir()
 

Index | Next | Previous | View Flat


tismer at stackless

Apr 26, 2012, 2:30 PM


Views: 437
Permalink
package imports, sys.path and os.chdir()

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>
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/

_______________________________________________
Python-Dev mailing list
Python-Dev [at] python
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: http://mail.python.org/mailman/options/python-dev/list-python-dev%40lists.gossamer-threads.com

Subject User Time
package imports, sys.path and os.chdir() tismer at stackless Apr 26, 2012, 2:30 PM
    Re: package imports, sys.path and os.chdir() ncoghlan at gmail Apr 26, 2012, 5:39 PM
    Re: package imports, sys.path and os.chdir() tismer at stackless Apr 27, 2012, 7:39 AM
        Re: package imports, sys.path and os.chdir() brett at python Apr 27, 2012, 1:00 PM
    Re: package imports, sys.path and os.chdir() v+python at g Apr 27, 2012, 1:37 PM
    Re: package imports, sys.path and os.chdir() tismer at stackless Apr 27, 2012, 2:21 PM
    Re: package imports, sys.path and os.chdir() ncoghlan at gmail Apr 28, 2012, 1:08 AM
    Re: package imports, sys.path and os.chdir() benjamin at python Apr 28, 2012, 6:35 AM
    Re: package imports, sys.path and os.chdir() rdmurray at bitdance Apr 28, 2012, 9:16 AM
        Re: package imports, sys.path and os.chdir() brett at python Apr 28, 2012, 12:20 PM
        Re: package imports, sys.path and os.chdir() pje at telecommunity Apr 28, 2012, 8:41 PM
    Re: package imports, sys.path and os.chdir() brett at python Apr 28, 2012, 12:16 PM
    Re: package imports, sys.path and os.chdir() brett at python Apr 28, 2012, 12:17 PM
    Re: package imports, sys.path and os.chdir() tjreedy at udel Apr 28, 2012, 7:02 PM
    Re: package imports, sys.path and os.chdir() steve at pearwood Apr 28, 2012, 8:20 PM
    Re: package imports, sys.path and os.chdir() ncoghlan at gmail Apr 28, 2012, 10:05 PM
    Re: package imports, sys.path and os.chdir() tismer at stackless Apr 29, 2012, 4:14 AM
    Re: package imports, sys.path and os.chdir() tismer at stackless Apr 29, 2012, 6:37 AM

  Index | Next | Previous | View Flat
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.