Search path on Windows

Matt Gerrans mgerrans at mindspring.com
Mon Aug 19 22:49:45 EDT 2002


> How do you tell the pyhton interpreter where to find modules that you
> want to import?
> Say, I have a file mymodule1.py in directory C:\home\MyPythonCode

At runtime (in the interpreter) you can do this:
>>> import sys
>>> sys.path.append(r'C:\home\MyPythonCode')

> In another file, I have the statement:
> from mymodule1 import *

Be careful about using this "import *" technique.   You can get lots of
unexpected behavior.   For example if you import two modules this way and
they both have anything with the same name, you will only get one of that
thing.   With some exceptions (which, as an admitted newbie, to can defer
'til later), you are much better off keeping things explicit, like so:

import mymodule1
mymodule1.foo(1,2,3)

> How do I configure Python 2.2 for Windows, so that it will know where
> to find mymodule1.py?

Windows isn't much different than other platforms in this respect, except
that you can also add things (classpath modifications) to the registry --
but I would avoid doing this.   Instead, use the platform-independent
methods:
- Setting the PYTHONPATH environment variable (in Windows NT/2K/XP do this
under "System Properties/Advanced/Environment Variables," in others, use
autoexec.bat).   The PYTHONPATH variable is a semicolon-separated (on
Windows -- most others use a colon as separator) list of additional
directories for python to search for modules.
- Create any number of files in the Python directory that has the extension
.PTH.   In each of these files just add a line for each additional path you
want searched.   This allows you to set up groups of paths by project (at
least, that's my guess of the intent).  By Python directory, I mean where
you installed Python, probably c:\Python22, unless you have a different
version, or decided to install elsewhere.

> Does it matter that I am running the scripts from emacs?

I think not.   You might have to close emacs and start it again before it
will discover the changed environment variables (unless it is watching for a
nd responding correctly to the Windows settings change broadcast message,
which I doubt).

> I have the book "Programming Python" by Mark Lutz but I can't seem to
> find the information I need.

If you are just starting out, "Learning Python" is probably a better choice.
It only covers Python 1.5.1, so you'll have additional material to learn
when you are finished, but it is a well-written book and will give you a
good start.







More information about the Python-list mailing list