Python on Windows — Setting PYTHONPATH Environment Variable

by Yuji

What is the PYTHONPATH environment variable?

The PYTHONPATH environment variable is the list of folders that the import statement will look through when attempting to resolve a module name.

A per session Python Path is visible in sys.path (per python shell session)

import sys
print sys.path # returns list of directories

# now, we can import pyton modules in C:\Foobar

The above solution requires appending to the python path every session. If we don’t want that, we can edit the PYTHONPATH environment variable.

How to set the PYTHONPATH environment variable on Windows XP, Vista, Windows 7, etc.

For Windows the PYTHONPATH is set in the windows registry.

Run regedit

First, open up “Run” from the start menu (Ctrl + R).

Browse to the PythonPath folder

Browse to HKEY_LOCAL_MACHINE -> SOFTWARE -> Python -> PythonCore -> [ Version Number ] -> PythonPath.

Don’t alter the pythonpath you see here.

Create new key

Right click the PythonPath folder and select New –> Key

Name it what you like.

Select the new folder and right click the one blank value sitting on the right side of the screen and select Modify.

For the value field, add your new paths separated by ” ; ”

Run python and try an import.

If that doesn’t work

Updated 1/2014

There are people in comments below with a ton of different solutions. I know that windows is at 8 now, and I haven’t tested with 8.

An alternative method is described here that doesn’t use REGEX. My gut tells me it may be more reliable method.