Search path on Windows

Peter Hansen peter at engcorp.com
Mon Aug 19 22:26:21 EDT 2002


Jason Taylor wrote:
> 
> This is a newbie question.
> 
> 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
> 
> In another file, I have the statement:
> 
> from mymodule1 import *
> 
> How do I configure Python 2.2 for Windows, so that it will know where
> to find mymodule1.py?

There are various alternatives, including manually sys.path.append'ing
as Michael showed you.  You can also use .pth files (see the file
python22/lib/site.py for some notes on that), or use the PYTHONPATH
environment variable.

On another note, it is almost never a good idea to use the form 
"from module import *" as you have done above.  While it may look
convenient, and is used in a few standard modules (such as wxPython
for example), when used casually it will lead to serious problems
down the road, especially for newcomers to Python who might not
yet understand the implications.

Until you understand why I say that (look through the list archives
for many discussions on the topic if you're interested), I suggest 
you switch to the safe "import module" form, and prefix the items 
from that module with the module name whenever you use them, as in 
"module.xxxx".  It's worth the extra effort to be explicit like that.

-Peter



More information about the Python-list mailing list