PYTHONPATH in Windows

Diez B. Roggisch deets at nospam.web.de
Sat Nov 29 13:39:35 EST 2008


waltbrad schrieb:
> 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.

Both result in the same - an environment-variable being added.

That no shows up when you do that for the first time isn't too much 
surprising - it doesn't exist, but you can create it.


However, I would strongly recommend *not* relying on a PYTHONPATH-variable.

Instead, go & install virtualenv. Then create a virtualenv for you 
django-project, and install django into it (however that is accomplished).

Then, go & install your codebase for your app into it as well. 
Preferably as egg-link. Should be easy enough with a minimal setup.py 
like this:

from setuptools import setup, find_packages
setup(
     name = "HelloWorld",
     version = "0.1",
     packages = find_packages(),
)


The advantage of this approach is that you can have several versions of 
your software installed in distinct VEs, for example for 
debugging/patching a released version, without having to meddle with the 
  PYTHONPATH everytime.

The important thing to remember ist just to always either activate the 
proper VE, or use the full path to the python executable inside it.

Diez



More information about the Python-list mailing list