How to add to the PYTHONPATH in Windows, so it finds my modules/packages?

PythonWindowsEnvironment VariablesPythonpath

Python Problem Overview


I have a directory which hosts all of my Django apps (C:\My_Projects). I want to add this directory to my PYTHONPATH so I can call the apps directly.

I tried adding C:\My_Projects\; to my Windows Path variable from the Windows GUI (My Computer > Properties > Advanced System Settings > Environment Variables). But it still doesn't read the coltrane module and generates this error:

>Error: No module named coltrane

Python Solutions


Solution 1 - Python

You know what has worked for me really well on windows.

My Computer > Properties > Advanced System Settings > Environment Variables >

Just add the path as C:\Python27 (or wherever you installed python)

OR

Then under system variables I create a new Variable called PythonPath. In this variable I have C:\Python27\Lib;C:\Python27\DLLs;C:\Python27\Lib\lib-tk;C:\other-folders-on-the-path

enter image description here

This is the best way that has worked for me which I hadn't found in any of the docs offered.

EDIT: For those who are not able to get it, Please add

> C:\Python27;

along with it. Else it will never work.

Solution 2 - Python

Windows 7 Professional I Modified @mongoose_za's answer to make it easier to change the python version:

  1. [Right Click]Computer > Properties >Advanced System Settings > Environment Variables

  2. Click [New] under "System Variable"

  3. Variable Name: PY_HOME, Variable Value:C:\path\to\python\version enter image description here

  4. Click [OK]

  5. Locate the "Path" System variable and click [Edit]

  6. Add the following to the existing variable:

    %PY_HOME%;%PY_HOME%\Lib;%PY_HOME%\DLLs;%PY_HOME%\Lib\lib-tk; enter image description here

  7. Click [OK] to close all of the windows.

As a final sanity check open a command prompt and enter python. You should see

>python [whatever version you are using]

If you need to switch between versions, you only need to modify the PY_HOME variable to point to the proper directory. This is bit easier to manage if you need multiple python versions installed.

Solution 3 - Python

From Windows command line:

set PYTHONPATH=%PYTHONPATH%;C:\My_python_lib

To set the PYTHONPATH permanently, add the line to your autoexec.bat. Alternatively, if you edit the system variable through the System Properties, it will also be changed permanently.

Solution 4 - Python

Just append your installation path (ex. C:\Python27\) to the PATH variable in System variables. Then close and open your command line and type 'python'.

Solution 5 - Python

These solutions work, but they work for your code ONLY on your machine. I would add a couple of lines to your code that look like this:

import sys
if "C:\\My_Python_Lib" not in sys.path:
    sys.path.append("C:\\My_Python_Lib")

That should take care of your problems

Solution 6 - Python

Adding Python and PythonPath to the Windows environment:

  1. Open Explorer.

  2. Right-click 'Computer' in the Navigation Tree Panel on the left.

  3. Select 'Properties' at the bottom of the Context Menu.

  4. Select 'Advanced system settings'

  5. Click 'Environment Variables...' in the Advanced Tab

  6. Under 'System Variables':

  7. Add

  • PY_HOME

            C:\Python27
    
  • PYTHONPATH

            %PY_HOME%\Lib;%PY_HOME%\DLLs;%PY_HOME%\Lib\lib-tk;C:\another-library
    
  1. Append
  • path

            %PY_HOME%;%PY_HOME%\Scripts\
    

Solution 7 - Python

The easier way to set the path in python is : click start> My Computer >Properties > Advanced System Settings > Environment Variables > second windows >

enter image description here

select Path > Edit > and then add ";C:\Python27;C:\Python27\Scripts"

link :http://docs.python-guide.org/en/latest/starting/install/win/

Solution 8 - Python

The easiest way to do that successfully, is to run the python installer again (after the first installation) and then:

  1. choose Modify.
  2. check the optional features which you want and click Next.
  3. here we go, in "Advanced Options" step you must see an option saying "Add Python to environment variables". Just check that option and click Install. 3rd step When the installation is completed, python environment variables are added and you can easily use python everywhere.

Solution 9 - Python

You need to add to your PYTHONPATH variable instead of Windows PATH variable.

http://docs.python.org/using/windows.html

Solution 10 - Python

You can also add a .pth file containing the desired directory in either your c:\PythonX.X folder, or your \site-packages folder, which tends to be my preferred method when I'm developing a Python package.

See here for more information.

Solution 11 - Python

import sys
sys.path.append("path/to/Modules")
print sys.path

This won't persist over reboots or get translated to other files. It is however great if you don't want to make a permanent modification to your system.

Solution 12 - Python

The PYTHONPATH environment variable is used by Python to specify a list of directories that modules can be imported from on Windows. When running, you can inspect the sys.path variable to see which directories will be searched when you import something.

To set this variable from the Command Prompt, use: set PYTHONPATH=list;of;paths.

To set this variable from PowerShell, use: $env:PYTHONPATH=’list;of;paths’ just before you launch Python.

Setting this variable globally through the Environment Variables settings is not recommended, as it may be used by any version of Python instead of the one that you intend to use. Read more in the Python on Windows FAQ docs.

Solution 13 - Python

This question needs a proper answer:

Just use the standard package site, which was made for this job!

and here is how (plagiating my own answer to my own question on the very same topic):


  1. Open a Python prompt and type
>>> import site
>>> site.USER_SITE
'C:\\Users\\ojdo\\AppData\\Roaming\\Python\\Python37\\site-packages'
...

(Alternatively, call python -m site --user-site for the same effect.)

  1. Create this folder if it does not exist yet:
...
>>> import os
>>> os.makedirs(site.USER_SITE)
...

(Or, in Bash, your preferred variant of makedirs -p $(python -m site --user-site).)

  1. Create a file sitecustomize.py (with exactly this filename, or it won't work) in this folder containing the content of FIND_MY_PACKAGES, either manually or using something like the following code. Of course, you have to change C:\My_Projects to the correct path to your custom import location.
...
>>> FIND_MY_PACKAGES = """
import site
site.addsitedir(r'C:\My_Projects')
"""
>>> filename = os.path.join(site.USER_SITE, 'sitecustomize.py')
>>> with open(filename, 'w') as outfile:
...     print(FIND_MY_PACKAGES, file=outfile)

And the next time you start Python, C:\My_Projects is present in your sys.path, without having to touch system-wide settings. Bonus: the above steps work on Linux, too!


Why does this work?

From the documentation of standard library package site:

> [Then] an attempt is made to import a module named sitecustomize, which can perform arbitrary site-specific customizations. [...].

So if you create a module named sitecustomize anywhere in PYTHONPATH, package site will execute it at Python startup. And by calling site.addsitedir, the sys.path can be safely extended to your liking.

Solution 14 - Python

In Python 3.4 on windows it worked when I added it to PATH enviroment variable instead of PYTHONPATH. Like if you have installed Python 3.4 in D:\Programming\Python34 then add this at the end of your PATH environment variable

;D:\Programming\Python34

Close and reopen command prompt and execute 'python'. It will open the python shell. This also fixed my Sublime 3 issue of 'python is not recognized as an internal or external command'.

Solution 15 - Python

The python 2.X paths can be set from few of the above instructions. Python 3 by default will be installed in C:\Users<Name>\AppData\Local\Programs\Python\Python35-32
So this path has to be added to Path variable in windows environment.

Solution 16 - Python

I got it worked in Windows 10 by following below steps.

Under environment variables, you should only add it under PATH of "System Variables" and not under "User Variables". This is a great confusion and eats time if we miss it.

Also, just try to navigate to the path where you got Python installed in your machine and add it to PATH. This just works and no need to add any other thing in my case.I added just below path and it worked.

> C:\Users\YourUserName\AppData\Local\Programs\Python\Python37-32

Most important, close command prompt, re-open and then re-try typing "python" to see the version details. You need to restart command prompt to see the version after setting up the path in environment variables.

After restarting, you should be able to see the python prompt and below info when typing python in command prompt:

On typing python in command prompt

Solution 17 - Python

To augment PYTHONPATH, run regedit and navigate to KEY_LOCAL_MACHINE \SOFTWARE\Python\PythonCore and then select the folder for the python version you wish to use. Inside this is a folder labelled PythonPath, with one entry that specifies the paths where the default install stores modules. Right-click on PythonPath and choose to create a new key. You may want to name the key after the project whose module locations it will specify; this way, you can easily compartmentalize and track your path modifications.

thanks

Solution 18 - Python

For anyone trying to achieve this with Python 3.3+, the Windows installer now includes an option to add python.exe to the system search path. Read more in the docs.

Solution 19 - Python

This PYTHONPATH variable needs to be set for ArcPY when ArcGIS Desktop is installed.

PYTHONPATH=C:\arcgis\bin (your ArcGIS home bin)

For some reason it never was set when I used the installer on a Windows 7 32-bit system.

Solution 20 - Python

Maybe a little late, but this is how you add the path to the Windows Environment Variables.

  1. Go to the Environment Variables tab, you do this by pressing Windows key + Pausa inter.

  2. Go to Advanced System Settings.

  3. Click on Environment Variables.

  4. On the lower window search for the 'Path' value.

  5. Select it

  6. Click on Edit

  7. In the end of the line add your instalation folder and the route to 'Scripts' folder.

  8. Click ok, aceptar etc.

You're done, enter cmd and write python from any location of your drive, it should enter the Python program.

Example with my pc (I have Python34)

EXISTING_LINES;C:\Python34;C:\Python34\Scripts\

Hope it helps.

Greetings from Bogotá

Solution 21 - Python

You can set the path variable for easily by command prompt.

  1. Open run and write cmd

  2. In the command window write the following: set path=%path%;C:\python36

  3. press enter.

  4. to check write python and enter. You will see the python version as shown in the picture.

enter image description here

Solution 22 - Python

While this question is about the 'real' Python, it did come up in a websearch for 'Iron Python PYTHONPATH'. For Iron Python users as confused as I was: It turns out that Iron Python looks for an environment variable called IRONPYTHONPATH.

Linux/Mac/POSIX users: Don't forget that not only does Windows use \ as path separators, but it also uses ; as path delimiters, not :.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiondarrenView Question on Stackoverflow
Solution 1 - PythondarrenView Answer on Stackoverflow
Solution 2 - PythonDelicia BrummittView Answer on Stackoverflow
Solution 3 - PythonYuval AdamView Answer on Stackoverflow
Solution 4 - PythonPaulino IIIView Answer on Stackoverflow
Solution 5 - PythoninspectorG4dgetView Answer on Stackoverflow
Solution 6 - PythonMr. PolywhirlView Answer on Stackoverflow
Solution 7 - PythonBamara CoulibalyView Answer on Stackoverflow
Solution 8 - PythonHamidrezaView Answer on Stackoverflow
Solution 9 - PythonTaoView Answer on Stackoverflow
Solution 10 - PythonbrentlanceView Answer on Stackoverflow
Solution 11 - PythonGajendra D AmbiView Answer on Stackoverflow
Solution 12 - PythonMWOJOView Answer on Stackoverflow
Solution 13 - PythonojdoView Answer on Stackoverflow
Solution 14 - PythonOctaneView Answer on Stackoverflow
Solution 15 - PythonPavan EbbadiView Answer on Stackoverflow
Solution 16 - PythonRavi Shankar KotaView Answer on Stackoverflow
Solution 17 - PythonBrajendu Kumar DasView Answer on Stackoverflow
Solution 18 - PythonAndreas BergströmView Answer on Stackoverflow
Solution 19 - PythonDan RandolphView Answer on Stackoverflow
Solution 20 - PythonsandatomoView Answer on Stackoverflow
Solution 21 - PythonAbdur RahmanView Answer on Stackoverflow
Solution 22 - PythonMichael ScheperView Answer on Stackoverflow