changing sys.path

Steven D'Aprano steve+comp.lang.python at
Wed Feb 1 19:51:34 EST 2012

On Wed, 01 Feb 2012 17:47:22 +0000, Andrea Crotti wrote:

> Yes they are exactly the same, because in that file I just write exactly
> the same list,
> but when modifying it at run-time it doesn't work, while if at the
> application start
> there is this file everything works correctly...
> That's what really puzzles me.. What could that be then?

Are you using IDLE or WingIDE or some other IDE which may not be 
honouring sys.path? If so, that's a BAD bug in the IDE.

Are you changing the working directory manually, by calling os.chdir? If 
so, that could be interfering with the import somehow. It shouldn't, but 
you never know...

Are you adding absolute paths or relative paths?

You say that you get an ImportError, but that covers a lot of things 
going wrong. Here's a story. Could it be correct? I can't tell because 
you haven't posted the traceback.

When you set site-packages/my_paths.pth you get a sys path that looks 
like ['a', 'b', 'fe', 'fi', 'fo', 'fum']. You then call "import spam" 
which locates b/ and everything works.

But when you call sys.path.extend(['a', 'b']) you get a path that looks 
like ['fe', 'fi', 'fo', 'fum', 'a', 'b']. Calling "import spam" locates 
some left over junk file, fi/ or fi/spam.pyc, which doesn't 
import, and you get an ImportError.


More information about the Python-list mailing list