PYTHONPATH in Windows

W

waltbrad

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.
 
D

Diez B. Roggisch

waltbrad said:
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
 
W

waltbrad

waltbrad schrieb:












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

This just sounds like you're making everything even more complicated.
Am I going to have to uninstall Python and Django and erase everything
I've done so far to use this?

I'm not real great at python. I know a little bit, enough to follow
what's going on with django so far, but it's all uphill.

But it sounds like your solution is to stop everything and learn about
another package before I can continue learning about django.

I got on the web and tried to look for a tutorial on this package. I
sort of understand what is being said: All this technology is
constantly being upgraded and the upgrades can break the code you've
already written and got working. So, I can see the importance of it.
It sort of copies over the code you are currently using in a project/
app and insulates it from upgrades.

But what is the learning curve on this? Being a windows user my
inferiority complex is aggravated everytime I have to translate what
is being done in a *nix system.
 
M

Martin v. Löwis

But no PYTHONPATH variable shows up in my environment settings.

To answer a long question with a single sentence: just add the variable,
and be done with it.

Regards,
Martin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

PYTHONPATH 8
PYTHONPATH: dev and prod 7
PYTHONPATH var 1
Using PythonPath under Windows Vista. 3
PYTHONPATH and module names 6
PYTHONPATH 10
PYTHONPATH vs PATH? 0
PYTHONPATH not working on Windows XP (?) 2

Members online

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,705
Latest member
Stefkari24

Latest Threads

Top