sys.path and os.chdir

ugodiggi Ugo_DiGirolamo at InVision.iip.com
Fri Aug 23 09:22:59 EDT 2002


I have the following script:

## START test0.py
import os, sys

os.chdir(r'D:\temp\temp1')
cwd = os.getcwd()
print sys.path
print os.getcwd()
import test1
print
print 'Done'

## END   test0.py


## START test1.py
pass
## END   test1.py


test0.py is in D:\temp
test1.py is in D:\temp\temp1


I try to run the script as follows:

C:\>c:

C:\>cd C:\

C:\>python D:\temp\test0.py
['D:\\temp', 'C:\\', 'D:\\python22\\DLLs', 'D:\\python22\\lib',
'D:\\python22\\lib\\lib-tk', 'D:\\python22',
'D:\\python22\\lib\\site-packages',
'D:\\python22\\lib\\site-packages\\Numeric']
D:\temp\temp1
Traceback (most recent call last):
  File "D:\temp\test0.py", line 8, in ?
    import test1
ImportError: No module named test1


And as follows:

D:\>D:
D:\>cd Temp
D:\Temp>python test0.py
['', 'D:\\Temp', 'D:\\python22\\DLLs', 'D:\\python22\\lib',
'D:\\python22\\lib\\lib-tk', 'D:\\python22',
'D:\\python22\\lib\\site-packages',
'D:\\python22\\lib\\site-packages\\Numeric']
D:\temp\temp1

Done

D:\Temp>



Obviously the problem is caused by the fact that my sys.path contains
'' when I invoke the script from the same dir, but contains the full
path of the script 'D:\\temp' when I invoke the script from another
dir.

So, calling os.chdir has no longer any effect when running the script
from another dir.

In order to fix it, I've tried to add '.' to my PYTHONPATH but it does
not solve the problem.

I guess I might explicitly append '' to sys.path in the script, but
this is not exactly a desirable solution (I would need to do it on
each script!).

Am I doing anything wrong?

Thank you!

Cheers

Ugo



BTW, I'm running 
Python 2.2.1 (#34, Apr  9 2002, 19:34:33) [MSC 32 bit (Intel)] on
win32



More information about the Python-list mailing list