I have some modules in one directory path that I want to import into the python shell while in another path. I set the PYTHONPATH variable in the shell
c:\Users\me\Documents> set PYTHONPATH="C:\Users\me\Documents\sandbox\puzzler3\build\lib"
c:\Users\me\Documents> set | grep PYTHONPATH
PYTHONPATH="C:\Users\me\Documents\sandbox\puzzler3\build\lib"
From the default directory \Users\me\Documents\poly, I do the following
poly> python
>>> import sys
sys.path
Instead of seeing the PYTHONPATH path in the list, I see a mangled concatenation of the current default path with PYTHONPATH. Changing to a different default directory gives the same results but with the new directory path.
'c:\\Users\\me\\Documents\\poly\\"C:\\Users\\me\\Documents\\sandbox\\puzzler3\\build\\lib"'
Naturally, trying to import modules from the desired location fails. Unsetting PYTHONPATH, restarting python, and printing out sys.path shows a list without the current default directory.
Why is python performing this concatenation instead of simply including the contents of PYTHONPATH as an element is sys.path?
2 Answers 2
Remove the quotes.
set PYTHONPATH=C:\Users\me\Documents\sandbox\puzzler3\build\lib
It seems that the string within quotes is interpreted as a relative path (a filename perhaps?), so it concatenates it to the CWD.
Comments
If you are already having PYTHONPATH set and want to add your library as well to the path do. (Note. With out quotes)
set PYTHONPATH=%PYTHONPATH%;C:\Users\me\Documents\sandbox\puzzler3\build\lib