Customizing the python search path depending on source directory

Peter Schwalm peter.schwalm at epost.de
Wed Apr 7 21:34:07 EDT 2004


Peter Hansen <peter at engcorp.com> wrote in message news:<hsWdncv_b86aY-7d4p2dnA at powergate.ca>...
> Peter Schwalm wrote:
> 
> > Peter Hansen <peter at engcorp.com> wrote in message news:> >
> > Thank you for your answer. But I'm afraid it's not the source
> > directory but the current working directory. You can see the
> > difference if you copy the passage
> > 
> >     "for ix1, p1 in enumerate(sys.path): print "%02.02d: %s" % (ix1,
> > p1)"
> > 
> > to a "normal script". If you start that "normal script" from a
> > directory other than the source directory, you can see that both
> > directories are included in sys.path. If you run this code inside
> > sitecustomize.py this is not the case.
> 
> On the contrary.  When I start that from another directory which
> is *not* in the path, I do not see that directory in the sys.path
> at all, but merely the directory in which the script itself is
> stored.  This is the defined behaviour of the Python sys.path
> stuff and you must be misinterpreting something.
> 
> The current directory is *not* added to sys.path, unless it
> just happens that the main script is in the current directory.
> 
Again, thank you for your answer. 
This answer lets me suspect that you have a different version of
python installed. Mine is 2.3.2 for win32 (activepython).

To show you what I observe I have modified my "sitecustomize.py"
slightly and have copied the unmodified text do script "test1.py"

The source for both looks as follows:

#-------------------------------------------------------------------------
import sys
import os
print "--->", __file__, "..."
print 64 * "-"
print "python-version=", sys.version
try:
    print "argv=", sys.argv
except AttributeError:
    print ""
    print "sys.argv does still not exist!"

try:
    print "sys.modules[\"__main__\"]=",
sys.modules["__main__"].__file__
except AttributeError:
    print ""
    print "sys.modules[\"__main__\"].__file__ does still not exist"

# ... shows that source directory is still not in sys.path
print "sys.path="
for ix1, p1 in enumerate(sys.path):    print "%02.02d: %s" % (ix1, p1)

print 64 * "-"
print "<---", __file__, "..."
#-------------------------------------------------------------------------

Here is the output from test1.py when run it's own source directory:

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

E:\temp>

And here is the output from test1.py when run from a different
directory (from subdirectory sub1):


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

E:\temp\sub1>

As you can see in the second output, the source directory is *not*
included in sys.path during the processing of sitecustomize.py, but
only during the processing of the "normal script" test1.py.

Does the output look different if you run these programs in your
environment?

Thank you
Peter



More information about the Python-list mailing list