[Tutor] Re: Two (not so) dumb questions

Jeff Shannon jeff at ccvcorp.com
Fri Sep 5 11:09:19 EDT 2003


RASTM2 at aol.com wrote:
> 
>> Date: Tue, 2 Sep 2003 16:29:00 +0700
>> From: "Robin Hood"
>>
>> 1. How do you set the PATH environment variable for python under 
>> windows 98
>> (So that I can import modules which aren't located in the same folder as
>> python itself)
> 
> Open the autoexec.bat file. (Keep in mind that there may be
> nothing in this file to begin with and that's okay.)
> 
> On the line after the last entry in the file, type something like this
> (using your version of Python of course):
> 
> PATH=C:\Python23\;C:\Python23\lib;%path%
> 

Actually, this won't accomlish what the O.P. wanted, because $PATH is 
not used when Python is importing modules; $PYTHONPATH is.  The above 
line will ensure that Python is always on your path, so that you don't 
need to be in C:\Python23 to run it.  However, for Python to be able 
to find a module when that module is imported, it must be in a 
directory listed in sys.path, which is constructed from a variety of 
sources.  That includes (but is not necessarily limited to) the 
$PYTHONPATH environment variable and .pth files.  (Note that sys.path 
will also include the current working directory, so if you CD into a 
directory before running Python, you can import scripts in that 
directory.)

One *can* place a line in autoexec.bat like so:

PYTHONPATH=C:\Project1\PythonScripts;C:\Project2\Python

And scripts in those directories can then be found by import.  (The 
same can be accomplished by putting those two directories, one per 
line, in a .pth file in c:\python23, as well.)

Jeff Shannon
Technician/Programmer
Credit International




More information about the Tutor mailing list