[Tutor] possible import locations

Dave Kuhlman dkuhlman at rexx.com
Wed Jan 17 18:56:00 CET 2007


On Wed, Jan 17, 2007 at 06:07:19AM -0500, Kent Johnson wrote:
> Andre Engels wrote:
> > Is it possible to see from a python program where it searches for 
> > possible imports? Is it possible to import from another location than 
> > those? (Of course with an "if so, how" attached).
> > 
> > The issue is that the company I work for is switching providers. With 
> > the old provider (as well as in my local setting), Python automatically 
> > searched for imports in the current directory (the one where the program 
> > was run). With the new one, this does not seem to be the case, so I am 
> > getting ImportErrors all over the place.
> 
> sys.path is a list of locations that will be searched for imports. You 
> can add new entries to it with the usual list operations.
> 
> If you want to add '.' to sys.path always you could do this in a 
> sitecustomize.py module.
> - Create a directory Lib\site-packages if it doesn't already exist
> - Create a file site-packages\sitecustomize.py
> - Put your custom startup stuff in sitecustomize.py

You will also want to read about .pth files.  See this:

    http://docs.python.org/lib/module-site.html

And, of course, you can also read the source.  See site.py, which
on my machine is at:

    /usr/local/lib/python2.5/site.py

> 
> Is your new provider using a different OS? I remember a discussion long 
> ago about the current directory being in sys.path or not and it seemed 
> to vary depending on OS.

Right.  I'm on Linux (Debian GNU/Linux).  '.' is not on my
sys.path, but I can import from the current directory.

However, the empty string ('') is the first item in the list
sys.path.  Does the empty string mean the same thing as the
directory '.'?

Does anyone have a reference to a document that specifies
recommended policy for the use of the .pth files and the
sitecustomize.py file.  For example, in addition to Kent's
recommendation, you could also put sitecustomize.py in your current
directory, where it would only affect programs run from that
directory.  Which is recommended and when?

Also, the .pth file thing seems to be a way to give a package, when
it is installed, to hijack the import path by stuffing paths above
(earlier than) existing paths.  Is there a recommended policy on
this?  If so, where can I read up on it?

Dave

-- 
Dave Kuhlman
http://www.rexx.com/~dkuhlman


More information about the Tutor mailing list