How to add to the PYTHONPATH in Windows, so it finds my modules/packages?
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
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:
- [Right Click]Computer > Properties >Advanced System Settings > Environment Variables
- Click [New] under "System Variable"
- Variable Name: PY_HOME, Variable Value:C:\path\to\python\version
- Click [OK]
- Locate the "Path" System variable and click [Edit]
Add the following to the existing variable:
%PY_HOME%;%PY_HOME%\Lib;%PY_HOME%\DLLs;%PY_HOME%\Lib\lib-tk;
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
darren
Updated on January 20, 2022Comments
-
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 myPYTHONPATH
so I can call the apps directly.I tried adding
C:\My_Projects\;
to my WindowsPath
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
-
JinSnow over 7 yearsfor "no module named" error: stackoverflow.com/questions/23417941/…
-
Zeinab Abbasimazar over 6 yearsI used
site
module. -
Mahesh Jamdade about 5 yearsif you want to run python in command prompt it is answered well here I hope this helps stackoverflow.com/a/54618199/8253662
-
-
darren over 13 yearsWorked but everytime I open dos I had to set the path.
-
Mr Wednesday about 11 yearsI prefer to use
set path=%PATH%;%PYTHONPATH%;
-
Wei Yang over 10 yearsYou probably want to add
C:\Python27
in your path. -
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 over 10 yearsModifying
sys.path
like this is frowned upon and for good reasons. There are better ways of configuringsys.path
—PYTHONPATH
environment variable and.pth
files. -
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 over 10 yearsDos and editing autoexec.bat..! Have we suddenly slipped back into the 90s?
-
Tyler Brock over 10 yearsIt's also important to add C:\Python27\Scripts to the path so that installed scripts can be run from the shell.
-
Eryk Sun over 9 yearsWhy would you put
C:\Python27\Lib;C:\Python27\DLLs;C:\Python27\Lib\lib-tk
inPYTHONPATH
? Those are already configured. Directories for scripts go in the systemPATH
. Directories for libraries (that aren't installed tosite-packages
or the per-usersite-packages
) go inPYTHONPATH
. -
wpercy over 8 yearsThis is answering a completely different question than the one OP was asking.
-
Christopher M. over 8 yearsTried this and it worked... i would recommend anyone going through the same problem to try this.
-
Andrew over 8 yearsHe 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 about 8 yearsIt 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 almost 8 yearsTried this, it didn't do anything at all.
-
gosr almost 8 yearsI had to include %PYTHONPATH% in my PATH as well.
-
mjwrazor over 7 yearsI tried the accepted answer above and that didn't work so I would recommend this one.
-
Fares K. A. over 7 yearsThis 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 over 7 yearschanging system path changed nothing (deleting it too). But typing in admin cmd worked: ftype Python.File="C:\Python27\python.exe" "%1" %*
-
Alberto Perez about 7 yearsRemember restart your computer
-
Ringo about 7 yearsWhat is Pausa inter.?
-
neurotik about 7 yearsNo need to restart computer, just need to open up a new command prompt and it should work.
-
razor about 7 yearsThanks 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 almost 7 yearsBy "Variable Value:C:\path\to\python\version" do you mean the address of the directory that included python.exe?
-
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 almost 7 yearsI 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 almost 7 yearsIf you want to open this from the command line,
rundll32.exe sysdm.cpl,EditEnvironmentVariables
works. I put it in a batch fileenv.bat
. -
ntg over 6 yearsBefore 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 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 almost 6 yearsPython on Windows, appreciating node.js a little more right now
-
StackG over 5 yearsI disagree that you only have to do this once. I find that the extra path does not persist past the kernal restarting
-
Davidson Lima over 5 yearsYeah, 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 over 4 yearsThe 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 variablePYTHONPATH
, but Windows seems to be 'special'. -
Pramesh Bajracharya about 4 yearsThis is the way to go it seems. A good, cross-platform and long term solution. Great one!
-
Innovator-programmer over 3 yearsfor python3.8, Python38-32\Lib doesn't contain lib-tk. This means, isn't required to add lib-tk path in PYTHONPATH?
-
Soup Endless about 3 yearsMany thanks for this, this is the only way I've found to make this work for VS Community 2019!
-
Miroslav Avramov about 3 yearsSetting 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 about 3 years
-
Jack Griffin over 2 yearsI followed all the steps,It works, but I must launch sitecustomize.py every time.
-
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 variablesite.USER_SITE_ENABLED
(must beTrue
); maybe it is disabled in your environment. -
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 over 2 yearsThere 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 over 2 yearsI 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 bysite.USER_SITE
! Whatever code you place in there will be executed. To verify that the file was executed, you can check the contents ofsys.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 over 2 years@JackGriffin: the filename must be exactly
sitecustomize.py
(orusercustomize.py
), but not anything else. -
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 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.