Python Win installations and PYTHONPATH Confusion (WAS: Re: Python Path Blues)

Don Dwiggins dwig at advancedmp.net
Mon Aug 20 17:37:47 EDT 2001


Jeff Shannon writes:
>> What is important here is that this can be a BIG turn-off for beginners.
>> It took me more than a day of tinkering with the registry while trying to
>> make DCOracle2 and mxODBC packages for Python run correctly.  Then I
>> discovered it was all a matter of adding PYTHONPATH into the environment in
>> Win NT and Win ME (for Win ME through running msconfig  then going to
>> Environment tab or creating a PYTHONPATH setting in autoexec.bat which
>> updates the Win ME environment).

> Well, there are other ways than tinkering with the registry, too.  Haven't tried
> it myself, but I've heard that putting a python.pth file in the same directory
> as your python.exe, will cause each line of the .pth file to be interpreted as a
> path to add to sys.path.  On the other hand, I fiddled with my registry (on
> Win95/98) to add directories to my path, and, while I don't recall *exactly*
> what I did with it, it only took me a few tries to figure out.  I do agree that
> this could be better documented, though...  but I suspect that, due to the ease
> (and portability) of using .pth files, that it's not going to be a high
> priority.

FWIW, after various attempts, here's what I came up with that works for me:

In the Python installation directory, I put a file "dwig.pth" (name isn't
important, as long as it ends in ".pth"; I wanted to make it clear on
looking at the directory that I put it there), which contains:
  e:\MyPython
That is, it points to a directory where good stuff will be found.
**BTW, the line should end with "\n", not "\r\n".

In e:\MyPython, I put a file "sitecustomize.py" to add to the the path
  all the modules that I stick into the directory, coded as follows:
------------------------------------------------------------
### Customize Python's environment for this site.  Depends on having a file
### "*.pth" (with Unix line endings) in the Python installation directory
### that points to the directory containing this file.

## Append all first-level subdirectories to the path
import sys, os.path

def makepath(*paths):
    dir = os.path.join(*paths)
    return os.path.normcase(os.path.abspath(dir))

here = os.path.dirname(__file__)
try:
    names = os.listdir(here)
except os.error:
    pass
names = map(os.path.normcase, names)
names.sort()
for name in names:
    dir = makepath(here, name)
    if dir not in sys.path and os.path.isdir(dir):
        sys.path.append(dir)
------------------------------------------------------------
IIRC, I adapted the code from {Python install dir}\Lib\site.py.

Now, I just dump good stuff like McMillan's installer and wxPython into
e:\MyPython and hey presto! it's available.

Would something like this make a good entry for the cookbook?

-- 
Don Dwiggins                    "Solvitur Ambulando"
Advanced MP Technology
dwig at advancedmp.net





More information about the Python-list mailing list