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

1,698,942

Solution 1

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

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

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

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

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

Share:
1,698,942
darren
Author by

darren

Updated on January 20, 2022

Comments

  • darren
    darren over 2 years

    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

  • darren
    darren over 13 years
    Worked but everytime I open dos I had to set the path.
  • Mr Wednesday
    Mr Wednesday about 11 years
    I prefer to use set path=%PATH%;%PYTHONPATH%;
  • Wei Yang
    Wei Yang over 10 years
    You probably want to add C:\Python27 in your path.
  • Steve Koch
    Steve Koch over 10 years
    @PiotrDobrogost that link is broken for me now. Is this the application you recommend: [patheditor2.codeplex.com/](patheditor2 at codeplex)
  • Piotr Dobrogost
    Piotr Dobrogost over 10 years
    Modifying sys.path like this is frowned upon and for good reasons. There are better ways of configuring sys.pathPYTHONPATH environment variable and .pth files.
  • Piotr Dobrogost
    Piotr Dobrogost over 10 years
    @SteveKoch I'm not sure — you would have to ask project's maintener. I no longer use Path Editor as I switched to much better Rapid Environment Editor
  • C Mars
    C Mars over 10 years
    Dos and editing autoexec.bat..! Have we suddenly slipped back into the 90s?
  • Tyler Brock
    Tyler Brock over 10 years
    It's also important to add C:\Python27\Scripts to the path so that installed scripts can be run from the shell.
  • Eryk Sun
    Eryk Sun over 9 years
    Why would you put C:\Python27\Lib;C:\Python27\DLLs;C:\Python27\Lib\lib-tk in PYTHONPATH? Those are already configured. Directories for scripts go in the system PATH. Directories for libraries (that aren't installed to site-packages or the per-user site-packages) go in PYTHONPATH.
  • wpercy
    wpercy over 8 years
    This is answering a completely different question than the one OP was asking.
  • Christopher M.
    Christopher M. over 8 years
    Tried this and it worked... i would recommend anyone going through the same problem to try this.
  • Andrew
    Andrew over 8 years
    He probably did that because none of of it particularly clear or well organized in any of the documentation, or in any of the tutorials that I've found. It's all a bit too vague, which is strange considering how, back when I was a young chap and python was just getting started, python modeled itself as the ideal language for programming newbies..
  • Martin Tournoij
    Martin Tournoij about 8 years
    It is indeed a little late, since this answer was already provided five years ago :-) It's also not really what is asked here (since the question is about PYTHONPATH, not PATH)...
  • GreySage
    GreySage almost 8 years
    Tried this, it didn't do anything at all.
  • gosr
    gosr almost 8 years
    I had to include %PYTHONPATH% in my PATH as well.
  • mjwrazor
    mjwrazor over 7 years
    I tried the accepted answer above and that didn't work so I would recommend this one.
  • Fares K. A.
    Fares K. A. over 7 years
    This is the only solution that worked for me. Also, for those of you who do not have administrator rights, just follow the steps above under "User variables for [yourUsername]" and it will work just as well.
  • JinSnow
    JinSnow over 7 years
    changing system path changed nothing (deleting it too). But typing in admin cmd worked: ftype Python.File="C:\Python27\python.exe" "%1" %*
  • Alberto Perez
    Alberto Perez about 7 years
    Remember restart your computer
  • Ringo
    Ringo about 7 years
    What is Pausa inter.?
  • neurotik
    neurotik about 7 years
    No need to restart computer, just need to open up a new command prompt and it should work.
  • razor
    razor about 7 years
    Thanks darren.. I know this is an old thread but it helped me in setting up node js.. This worked.. I had Python 3.6 and by default it installs in Program Files. I tried installing it to Python36 after seeing your solution. It did not work. Then I uninstalled Python 3.6 and installed Python 2.7 to Python27 folder and it worked..I had to include %PYTHONPATH% in my PATH as well... Have you found out why this is so?
  • Reihan_amn
    Reihan_amn almost 7 years
    By "Variable Value:C:\path\to\python\version" do you mean the address of the directory that included python.exe?
  • Delicia Brummitt
    Delicia Brummitt almost 7 years
    @Reihan_amn I mean the wrapping directory that contains the lib, bin, libexec, etc. Typically these directories are named Python[VERSION_NUMBER]/, but I have also seen python/[VERSION_NUMBER]. Hope this helps
  • Jose' Vargas
    Jose' Vargas almost 7 years
    I tried this and it worked for me. I had to make one small adjustment which was to click New for each of these entries for Windows 10 when adding them to the PATH variable. %PY_HOME%;%PY_HOME%\Lib;%PY_HOME%\DLLs;%PY_HOME%\Lib\lib-tk; I also needed to add %PY_HOME%\Scripts so I had access to pip and the modules I install with pip such as pylint
  • aschultz
    aschultz almost 7 years
    If you want to open this from the command line, rundll32.exe sysdm.cpl,EditEnvironmentVariables works. I put it in a batch file env.bat.
  • ntg
    ntg over 6 years
    Before this, do echo %PYTHONPATH% if this gives you a path go on, otherwise, do e.g. set PYTHONPATH=.;C:\My_python_lib If you don't, windows will expand %PYTHONPATH% to empty string as expected, it will keep it as %PYTHONPATH% in the PYTHONPATH and everything will break! Sounds crazy but thats how win7 cmd works...
  • Joshua Burns
    Joshua Burns over 6 years
    +1 for the ability to set paths are run time. while arguably "hacky", when writing a proof of concept or a one-off job, this is perfect and leaves the system unmodified.
  • Eddie
    Eddie almost 6 years
    Python on Windows, appreciating node.js a little more right now
  • StackG
    StackG over 5 years
    I disagree that you only have to do this once. I find that the extra path does not persist past the kernal restarting
  • Davidson Lima
    Davidson Lima over 5 years
    Yeah, this was the only solution to me (Python 3.x). I really have no idea why %PYTHONPATH% isn't resolved on Path parameter. Call the Scripts' folder wasn't necessary.
  • Michael Scheper
    Michael Scheper over 4 years
    The question isn't about the PATH that DOS uses to find commands, but the Python path, i.e. sys.path in Python. In most operating systems, Python just uses the system environment variable PYTHONPATH, but Windows seems to be 'special'.
  • Pramesh Bajracharya
    Pramesh Bajracharya about 4 years
    This is the way to go it seems. A good, cross-platform and long term solution. Great one!
  • Innovator-programmer
    Innovator-programmer over 3 years
    for python3.8, Python38-32\Lib doesn't contain lib-tk. This means, isn't required to add lib-tk path in PYTHONPATH?
  • Soup  Endless
    Soup Endless about 3 years
    Many thanks for this, this is the only way I've found to make this work for VS Community 2019!
  • Miroslav Avramov
    Miroslav Avramov about 3 years
    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.
  • Miroslav Avramov
    Miroslav Avramov about 3 years
  • Jack Griffin
    Jack Griffin over 2 years
    I followed all the steps,It works, but I must launch sitecustomize.py every time.
  • ojdo
    ojdo over 2 years
    @JackGriffin: which file have you placed where? I just verified the steps in Windows 10 under Python 3.8.8. Inside my "My_Projects" folder, I placed a dummypackage.py with a function definition. And within a fresh (I)Python shell, import dummypackage succeeds immediately, without manual intervention. Check variable site.USER_SITE_ENABLED (must be True); maybe it is disabled in your environment.
  • Jack Griffin
    Jack Griffin over 2 years
    @ojdo: I have Python 3.9.5 on Kubuntu 21.04.The folder I want to 'be public', so to speak, contains a filebox.py file. I followed your instructions, got site.USERSITE path, created customizesite.py there, added the folder as you wrote.Launched customize.py. The folder is in sys.path.**>>>import filebox** : no errors. Closed python prompt and launched it again.The folder is no more in sys.path. In the console, if folder path is the working directory >>> import filebox works. Otherwise I got a ModuleNotFoundError: No module named 'filebox'.
  • Jack Griffin
    Jack Griffin over 2 years
    There is no site.USER_SITE_ENABLED on my system, but one site.ENABLE_USER_SITE.Guess they change it in this version.Its value is True.You stated that this works on Linux too.It does, but it's only temporary, at least on my system.
  • ojdo
    ojdo over 2 years
    I just verified the steps above on Linux (EndeavourOS, Python 3.9.9, but that should not make any difference). Please note that the file sitecustomize.py must be placed in the directory indicated by site.USER_SITE! Whatever code you place in there will be executed. To verify that the file was executed, you can check the contents of sys.path. I find the .../my_projects path appended to this list in each (I)Python session. You don't use any virtual environments, do you?
  • ojdo
    ojdo over 2 years
    @JackGriffin: the filename must be exactly sitecustomize.py (or usercustomize.py), but not anything else.
  • Jack Griffin
    Jack Griffin over 2 years
    @ojdo in the previous comment I wrote the name incorrectly.The file I created is actually sitecustomize.py.I appreciate your help.Will try again all ASAP.
  • ojdo
    ojdo over 2 years
    @JackGriffin, ok, then it is puzzling to me as to why these steps do not work for you. I have been using them since 2015 on various Windows and Linux environments without issue.