Customizing the python search path depending on source directory

Peter Schwalm peter.schwalm at epost.de
Thu Apr 8 17:06:53 EDT 2004


Peter Hansen <peter at engcorp.com> wrote in message news:<4dudnVzbRvwbyOjdRVn-sw at powergate.ca>...
> Peter Schwalm wrote:
> 
> ... can you confirm that you get identical results if in each
> place where you typed "test1.py" you instead typed the
> equivalent with, for example, "python test1.py"?
> 

I have run the programs with "python <scriptname>.py" now. For my
purposes the results look identical - the source directory is still
not accessible during the processing of sitecustomize.py.

Here are the outputs of the explicit invocations:

Invocation from source directory:


E:\temp>python test1.py
---> C:\Python23\lib\sitecustomize.pyc ...
----------------------------------------------------------------
python-version= 2.3.2 (#49, Oct 24 2003, 13:37:57) [MSC v.1200 32 bit
(Intel)]
argv=
sys.argv does still not exist!
sys.modules["__main__"]=
sys.modules["__main__"].__file__ does still not exist
sys.path=
00: C:\WINNT\System32\python23.zip
01: C:\Python23\Lib\site-packages\Pythonwin
02: C:\Python23\Lib\site-packages\win32
03: C:\Python23\Lib\site-packages\win32\lib
04: C:\Python23\Lib\site-packages
05: C:\Python23\DLLs
06: C:\Python23\lib
07: C:\Python23\lib\plat-win
08: C:\Python23\lib\lib-tk
09: C:\Python23
10: C:\Python23\lib\site-packages\PIL
----------------------------------------------------------------
<--- C:\Python23\lib\sitecustomize.pyc ...
---> test1.py ...
----------------------------------------------------------------
python-version= 2.3.2 (#49, Oct 24 2003, 13:37:57) [MSC v.1200 32 bit
(Intel)]
argv= ['test1.py']
sys.modules["__main__"]= test1.py
sys.path=
00: E:\temp
01: C:\WINNT\System32\python23.zip
02: C:\Python23\Lib\site-packages\Pythonwin
03: C:\Python23\Lib\site-packages\win32
04: C:\Python23\Lib\site-packages\win32\lib
05: C:\Python23\Lib\site-packages
06: C:\Python23\DLLs
07: C:\Python23\lib
08: C:\Python23\lib\plat-win
09: C:\Python23\lib\lib-tk
10: C:\Python23
11: C:\Python23\lib\site-packages\PIL
----------------------------------------------------------------
<--- test1.py ...

E:\temp>


Invocation from different directory:

E:\temp\sub1>python ..\test1.py
---> C:\Python23\lib\sitecustomize.pyc ...
----------------------------------------------------------------
python-version= 2.3.2 (#49, Oct 24 2003, 13:37:57) [MSC v.1200 32 bit
(Intel)]
argv=
sys.argv does still not exist!
sys.modules["__main__"]=
sys.modules["__main__"].__file__ does still not exist
sys.path=
00: C:\WINNT\System32\python23.zip
01: C:\Python23\Lib\site-packages\Pythonwin
02: C:\Python23\Lib\site-packages\win32
03: C:\Python23\Lib\site-packages\win32\lib
04: C:\Python23\Lib\site-packages
05: C:\Python23\DLLs
06: C:\Python23\lib
07: C:\Python23\lib\plat-win
08: C:\Python23\lib\lib-tk
09: C:\Python23
10: C:\Python23\lib\site-packages\PIL
----------------------------------------------------------------
<--- C:\Python23\lib\sitecustomize.pyc ...
---> ..\test1.py ...
----------------------------------------------------------------
python-version= 2.3.2 (#49, Oct 24 2003, 13:37:57) [MSC v.1200 32 bit
(Intel)]
argv= ['..\\test1.py']
sys.modules["__main__"]= ..\test1.py
sys.path=
00: E:\temp
01: C:\WINNT\System32\python23.zip
02: C:\Python23\Lib\site-packages\Pythonwin
03: C:\Python23\Lib\site-packages\win32
04: C:\Python23\Lib\site-packages\win32\lib
05: C:\Python23\Lib\site-packages
06: C:\Python23\DLLs
07: C:\Python23\lib
08: C:\Python23\lib\plat-win
09: C:\Python23\lib\lib-tk
10: C:\Python23
11: C:\Python23\lib\site-packages\PIL
----------------------------------------------------------------
<--- ..\test1.py ...

E:\temp\sub1>

By the way, *if I remember that correctly*: to be able to invoke
python scripts simply by name ("test1" instead of "python test1.py")
you have to do
two things:

1)  set the environment variable PATHEXT such that it contains
".py"(see sample output from set command)
   
   E:\temp>set pathext
   PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.PY;.py;.pyw;
            .pyc;.pys;.pyo
   E:\temp>

2) associate the file extension ".py" with c:\python23\python.exe
right click a .py-file in explorer, select "open with" (my translation
of the german "oeffnen mit") select python.exe as application and
check "open always with".

Thank you in advance
Peter Schwalm



More information about the Python-list mailing list