Python Path

David Bolen db3l at fitlinxx.com
Mon Aug 28 21:18:34 EDT 2000


"Arnaldo Riquelme" <javanet at dynacap.com> writes:

> How do I tell python to look for modules or files that are not
> in the path.
> 
> I'm on Win32 platform. Python resides on C:\Program Files\Python.
> I have a directory on d: where a store all my developement staff.
> 
> I have a file called formatp.py in d:\pylab, I want to be able to import
> that file
> from the interpreter without having to put formatp.py in the python path.

Well, you do need to put d:\pylab on the path (unless you want to
manually unmarshal the file into memory - definitely overkill), but
that's not quite as onerous as it sounds, since it can be done
dynamically.  Just add your new directory to sys.path (a list) at any
point while your code is executing, and then a normal import will find
the file.

For example:

    import sys

    sys.append('<newdir>')

    import <newfile>

If you want to override standard locations in the path, then you can
sys.insert(0,'<newdir>') to put it at the front of the path rather
than appending it to the end.

Alternatively, if d:\pylab is a standard location where you are
storing various scripts, then it's probably useful to include it in
your python path by default.  There are several ways to accomplish
this.  For one, you can set the PYTHONPATH environment variable, or
you could add that directory to any file called <name>.pth in the main
Python directory, which contains entries to be added to the path.

--
-- David
-- 
/-----------------------------------------------------------------------\
 \               David Bolen            \   E-mail: db3l at fitlinxx.com  /
  |             FitLinxx, Inc.            \  Phone: (203) 708-5192    |
 /  860 Canal Street, Stamford, CT  06902   \  Fax: (203) 316-5150     \
\-----------------------------------------------------------------------/



More information about the Python-list mailing list