Home Ask Login Register

Developers Planet

Your answer is one click away!

Jonathan Sachs February 2016

When/how does Python use PYTHONPATH

I'm having some trouble understanding how Python uses the PYTHONPATH environment variable. According to the documentation, the import search path (sys.path) is "Initialized from the environment variable PYTHONPATH, plus an installation-dependent default."

In a Windows command box, I started Python (v.2.7.6) and printed the value of sys.path. I got a list of pathnames, the "installation-dependent default."

Then I quit Python, set PYTHONPATH to .;./lib;, restarted Python, and printed os.environ['PYTHONPATH']. I got .;./lib; as expected. Then I printed sys.path. I think it should have been the installation-dependent default with .;./lib; added to the start or the end. Instead it was the installation-dependent default alone, as if PYTHONPATH were empty. What am I missing here?


Marcus Müller February 2016

It always uses PYTHONPATH. What happened is probably that you quit python, but didn't quit your console/command shell. For that shell, the environment that was set when the shell was started still applies, and hence, there's no PYTHONPATH set.

Post Status

Asked in February 2016
Viewed 1,994 times
Voted 8
Answered 1 times


Leave an answer

Quote of the day: live life