PYTHONPATH?

Magnus Lycka lycka at carmen.se
Fri Feb 24 05:31:46 EST 2006


Dr. Pastor wrote:
> Several Documents about Python refer to PYTHONPATH.

If you need to import libraries into Python, that don't reside in the
standard locations in your Python installation, you need to define
a PYTHONPATH environment variable in the operating system, which
points out this directory.

Defining this is done in the same way as defining PATH etc.

> I could not find such variable.

Do you need it for anything?

> (Python 2.4.2, IDLE 1.1.2, Windows XP)
> How should/could I nominate a Directory to be the local Directory?

import os
os.chdir('c:/somedir')

Another way is to start python from there, e.g. from the
command prompt (nothing beats a command prompt!) you would do:

cd some\local\directory
python myscript.py (... or just python ...)

Note that backslash in a Python string literal denotes an escape
sequence. In Python 'c:\temp' means 'c' ':' tab 'e' 'm' 'p', since
'\t' is the escape sequence for a tab. You can use 'c:/temp' (ok
for  Windows APIs) r'c:\temp' (r for raw string, i.e. don't use
escape sequences) or 'c:\\temp' (\\ is the escape sequence for
backslash).

Also note, that the way to use libraries from an arbitrary
directory without setting PYTHONPATH, is not with os.chdir,
but by doing this:

import sys
sys.path.append('c:/somedir')



More information about the Python-list mailing list