Python on Windows — Setting PYTHONPATH Environment Variable

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.


35 thoughts on “Python on Windows — Setting PYTHONPATH Environment Variable

    1. Then you might want to check to make sure your python installation is for either current user or all local users and not for a user that is not logged on.

      1. hanks for this, but i followed all the instructions to the letter, and it’s still giving me the ‘import error’ message.
        my only deviation was that i didnt see the python folder in the \HKEY_LOCAL_MACHINE\SOFTWARE folder. i saw the folder in the \HKEY_CURRENT_USER\SOFTWARE like you said

      2. whew!!! it worked!!! No error message when i imported it in my IDLE environment, not with my DOS environment. . I hope this helps others with similar issues

      3. I can’t find python folder in any of the folders in registry. Yet I have installed python for all users.

  1. Brilliant!! Thanks you very much for this support!! Think that I have been using windows for aprox 10 years and I didn’t know there exist something called ‘regedit’…

  2. I still cannot install a module in python: I have Windows 7 and Python 2.5, and I´m trying to install a module, swampy, that I downloaded following the instructions in the thinkpython textbook. I have no previous knowledge on computing and I´m not an English-speaker, but I think that I have to install a path (and I don´t know what is this) in the search path of python to be able to import the swampy modules. I have tried almost everything, but nothing works. I followed your instructions (thanks, by the way, they are very clear and easy to follow, even for me!), and now I have a beautiful new key named swampy in my python path folder, but I don´t know what it´s supposed I should do with that. Could anybody help me, please?
    Thanks a lot!

  3. This hasn’t worked for me. I suppose the actual value for “new_path” would be something like “C:\Python27;” ? If anybody is willing to do a gmail or AIM chat with me to help me with this it’d be extremely appreciated.

  4. Thanks for the instructions. They worked perfectly on my desktop(running win xp). However on my laptop the same procedure has refused to work! The laptop is running windows 7 and Python 2.7.3. does anyone have a solution?

  5. Is there no other way to do this ?

    I don’t like having to edit my registry.

    Why can’t I simply create a new PYTHONPATH environment or system variable :

    1. Right click My Computer icon
    2.Choose Properties from the context menu
    3.Click Advanced tab (Advanced system settings link in Vista)
    4. Click Environment variables
    5. Create the PYTHONPATH variable and give it a value of C:\Python27\Lib\site-packages\swampy-2.1.1

    wouldn’t the above solution work?

  6. I tried this, windows 7 64bit, home premium, couldn’t find the correct key under just software, was actually under \HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Python\PythonCore\2.7\PythonPath – I think this is because i installed the 32bit version of Python 2.7 to ensure full compatibility with pyGame, which i believe only works with the 32bit version of Python (please correct me if i’m wrong!) so it puts it under the Wow6432Node in the registry, rather than just where you would expect to find it.

    Hope this helps!

    1. I am also thankful to you, Alex!
      I tried this months ago and gave up, tried it again and this time I thankfully saw your comment! I figured it out and it works perfectly!

      for those that DON’T SEE THE PYTHON FOLDER! read the above comment!

  7. Thank you,Vinh Bui (@BuiNDVinh) . I’ve been trying the command “sys.path.append(‘c:\python2x’) ” but it didn’t work until I used sys.path.append(‘c:\python2x\Lib\site-packages\swampy-2.1.1′). That was when i was able ta import ‘mr’ swampy. Thanks once again and one love to Yuji Tomita.

    1. THANK YOU so VERY much, I have been struggling so much to get Swampy working to continue my book. I tried to set the PYTHONPATH variable as indicated on my Windows and found everything as indicated in the instructions (I put c:\python2x\Lib\site-packages\swampy-2.1.1 as my new path) and it still did not work. But the above append did work, THANKS again.

    2. did everything as explained above and beyond the blasted thing still wouldnt flipping
      work why mr swampy is so awkward to live with ? its really doing my head in

  8. thanks for this, but i followed all the instructions to the letter, and it’s still giving me the ‘import error’ message.
    my only deviation was that i didnt see the python folder in the \HKEY_LOCAL_MACHINE\SOFTWARE folder. i saw the folder in the \HKEY_CURRENT_USER\SOFTWARE.

  9. whew!!! it worked!!! No error message when i imported it in my IDLE environment, not with my DOS environment. . I hope this helps others with similar issues

  10. im getting
    >>> import swampy.TurtleWorld
    Traceback (most recent call last):
    File “”, line 1, in
    import swampy.TurtleWorld
    File “C:\Python33\Lib\site-packages\swampy-2.1.1\swampy\”, line 10, in
    from Gui import Callable
    ImportError: No module named ‘Gui’

    pleasee please Help.

  11. I finally figured out how to get it working.. I could not get it work with Python 3.3. I had to dl 2.7. Once i dled 2.7 i just used

    import sys
    from TurtleWorld import *

    and it worked perfectly. I hope this helps someone because i was stuck on this for like 2 days:/

  12. i’m using windows 8.and i think some of you may find problem on finding Python folder in HKEY_LOCAL_MACHINE\SOFTWARE.for this,open Wow6432Node.u’ll find it there.and for the newbies,in value data put the value as:C:\python23\Lib\site-packages\swampy-2.1.1…
    best of luck

  13. Right here is the right blog for anyone who hopes to understand this topic.

    You realize a whole lot its almost hard to argue with
    you (not that I really will need to…HaHa).
    You certainly put a brand new spin on a topic that’s been written about for years.
    Wonderful stuff, just great!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s