PYTHONPATH in Windows

waltbrad waltbrad at hotmail.com
Sat Nov 29 19:31:04 CET 2008


PYTHONPATH is a concept I've never been able to get straight.  I can't
see the difference between this and just setting paths in the Windows
environment variables. So, for the longest time I just never worried
about it.

Now, I'm going through James Bennett's "Practical Django Projects" and
the issue raises it's head again.  We need to set up PYTHONPATH so
that python can find the directories for import statements. Can this
just be done through the environment variables?  Bennett says:

"On Windows, the setup is a bit more involved, largely because
Windows, unlike UNIX-based systems, isn’t as friendly to command-line–
based programs. In the Control Panel’s System area, under the Advanced
tab, you can set environment variables. The PYTHONPATH variable should
already be set up with the initial value Python provided, and you can
add new directories to it (directories in the list should be separated
with semicolons)."

But no PYTHONPATH variable shows up in my environment settings.  This
website:

http://www.imladris.com/Scripts/PythonForWindows.html

says you need to alter PYTHONPATH in the windows directory:

" Now that you've taught Windows to find the python executable in the
python install directory, you'll need to tell it how to find your
python scripts saved in folders other than that one; otherwise, your
python import statements will fail because they won't know where to
look for the custom modules you wish to import. Possible module
locations are specified by the PYTHONPATH environment variable, which
is stored in the Windows registry.

"To augment PYTHONPATH, run regedit and navigate to KEY_LOCAL_MACHINE
\SOFTWARE\Python\PythonCore and then select the folder for the python
version you wish to use. Inside this is a folder labelled PythonPath,
with one entry that specifies the paths where the default install
stores modules. Right-click on PythonPath and choose to create a new
key. You may want to name the key after the project whose module
locations it will specify; this way, you can easily compartmentalize
and track your path modifications.

"Your new key will have one string value entry, named (Default). Right-
click on it and modify its value data; this should be text in the same
format as the Path environment variable discussed above--absolute
directory paths, separated by semicolons. If one project will use
modules from several directories, add them all to this list. (Don't
bother attempting to add more string value entries to your new key, or
to the original PythonPath key, since they will be ignored.) Once
these new registry entries are in place, your scripts' import
statements should work fine."

I don't know when this was written, they refer to WIN 2000 but not
XP.  Is this correct?  Do I go into the registry and create a key and
type the path into it's string value?

I get pretty cautious about playing around with the registry.




More information about the Python-list mailing list