os.chdir() and import

haaserd haaserd at yahoo.com
Wed Jun 13 22:38:30 CEST 2001


Under Windows and Python 2.1, I am sometimes able to use an
os.chdir('xxx') and then follow this with an import command
for a module located in the 'xxx' directory.

However, it seems that when the module doing the
os.chdir('xxx') and import-ing is executing because of an
execfile() command, this technique doesn't work and an
import error occurs.

A search of the documentation seems to indicate that the
import search path is determined only by the value of
sys.path ("...It [import] generally involves searching for a
``built-in'' module with the given name and then searching a
list of locations given as sys.path." -- Python Reference
Manual 6.11).  I think "generally" here may refer to the
unique processing used for packages, but...?

Can someone clear this up for me?  Should sys.path always be
modified instead of using os.chdir?

Roger Haase






More information about the Python-list mailing list