"ImportError: No module named site" on Windows
Solution 1
I've been looking into this problem for myself for almost a day and finally had a breakthrough. Try this:
Setting the PYTHONPATH / PYTHONHOME variables
Right click the Computer icon in the start menu, go to properties. On the left tab, go to Advanced system settings. In the window that comes up, go to the Advanced tab, then at the bottom click Environment Variables. Click in the list of user variables and start typing Python, and repeat for System variables, just to make certain that you don't have mis-set variables for PYTHONPATH or PYTHONHOME. Next, add new variables (I did in System rather than User, although it may work for User too): PYTHONPATH, set to C:\Python27\Lib.
PYTHONHOME
, set to C:\Python27.
Hope this helps!
Solution 2
Quick solution: set PYTHONHOME and PYTHONPATH and include PYTHONHOME on PATH
For example if you installed to c:\Python27
set PYTHONHOME=c:\Python27
set PYTHONPATH=c:\Python27\Lib
set PATH=%PYTHONHOME%;%PATH%
Make sure you don't have a trailing '\' on the PYTHON* vars, this seems to break it aswel.
Solution 3
Make sure your PYTHONHOME environment variable is set correctly. You will receive this error if PYTHONHOME is pointing to invalid location or to another Python installation you are trying to run.
Try this:
C:\>set PYTHONHOME=C:\Python27
C:\>python
Use
setx PYTHONHOME C:\Python27
to set this permanently for subsequent command prompts
Solution 4
I was having this issue after installing both Windows Python and Cygwin Python, and trying to run Cygwin Python from Cygwin. I solved it by export
ing PYTHONHOME=/usr/ and PYTHONPATH=/usr/lib/python2.7
Solution 5
Locate site.py and add its path in PYTHONPATH. This will solve your problem.
Related videos on Youtube
Comments
-
Mimminito over 1 year
I am trying to install Python for the first time. I downloaded the following installer from the Python website: Python 2.7.1 Windows Installer (Windows binary -- does not include source). I then ran the installer, selected 'All Users' and all was fine. I installed Python into the default location:
C:\Python27
Next, to test that Python was installed correctly, I navigated to my Python directory, and executed
python
in the windows CMD prompt. It returns me the following error:ImportError: No module named site
When I execute
python -v
I get the following:#installing zipimport hook
import zipimport # builtin
#installed zipimport hook
#ImportError: No module named site
#clear builtin._
#clear sys.path
#clear sys.argv
#clear sys.ps1
#clear sys.ps2
#clear sys.exitfunc
#clear sys.exc_type
#clear sys.exc_value
#clear sys.exc_traceback
#clear sys.last_type
#clear sys.last_value
#clear sys.last_traceback
#clear sys.path_hooks
#clear sys.path_importer_cache
#clear sys.meta_path
#clear sys.flags
#clear sys.float_info
#restore sys.stdin
#restore sys.stdout
#restore sys.stderr
#cleanup main
#cleanup [1] zipimport
#cleanup [1] signal
#cleanup [1] exceptions
#cleanup [1] _warnings
#cleanup sys
#cleanup builtin
#cleanup ints: 6 unfreed ints
#cleanup floatsWhen I do
dir C:\Python27\Lib\site.py*
I get the following:Directory of C:\Python27\Lib 13/11/2010 20:08 20,389 site.py 1 File(s) 20,389 bytes 0 Dir(s) 694,910,976 bytes free
Any ideas?
-
Tim W. over 12 yearsAssuming you didn't change the default install location, does the file C:/Python27/Lib/site.py exist on your computer?
-
Mimminito over 12 yearsYes, site.py is located in that folder
-
John Machin almost 12 years@Mimminito: I'm curious (mildly) why you disappeared for 9 months and just now accepted my answer, and (greatly) if you ever found out what the underlying problem was.
-
CMCDragonkai over 10 yearsI'm getting this problem currently. I have no idea why and I've scoured the internet for a solution. It keeps saying ImportError: No module named site no matter how I install python and what I put into my PATHs. I would like to know how python actually loads site.py. Obviously this is a relative/absolute path issue. And If I could just configure python's path directly without resorting to random stabs in the dark.
-
robert about 9 yearsI had this problem. I was invoking from Cygwin which had the unix-like path for
PYTHONPATH
. I converted it to a windows style path withexport PYTHONPATH=$(cygpath -w $PYTHONPATH)
and it works fine. Adding this note to help other people that may come to this page with the same problem. -
idbrii about 9 years@robert: You should add that as an answer.
-
KJP about 6 years@robert I had the opposite problem. I installed Python from Windows and wanted to run it under Cygwin. I needed to add
export PYTHONPATH=$(cygpath -u $PYTHONPATH)
to my .bashrc file.
-
-
David Heffernan over 12 yearspython.org installer is just fine
-
Mimminito over 12 yearsI installed Active Python on my laptop, and this installed just fine. But it does not work on my PC, i still get the same error
-
Rishi about 12 yearsAnd you need to append %PYTHONHOME% to your path variable as well for this to work.
-
Timotei over 11 yearsAha! This should be the answer to the question. For me, it was a PYTHONHOME variable set to an invalid path... Removed that, and now it WORKS! It seems Steam's Alien Swarm mod added that.
-
Piotr Dobrogost over 10 yearsSetting none of
PYTHONPATH/PYTHONHOME
variables should be necessary for Python to locatesite.py
module placed in theLib
folder. This answer is a workaround at best. -
Damien over 10 yearsEnvironment variables are a nightmare. I blame Linux.
-
Iain Samuel McLean Elder about 10 yearsUse
setx PYTHONHOME C:\Python27
to set this permanently for subsequent command prompts. Otherwise, you have to use the set command every time. -
Kurt almost 10 yearsOn Linux, try unsetting the pythonpath.
unset PYTHONPATH
or make sure that.
is not in thePYTHONPATH
. -
robert about 9 yearsHi this is probably not still an issue for you, but in case other people come with this problem, see my comment on the main question. Basically you need to convert
PYTHONPATH
to a windows-style path withexport PYTHONPATH=$(cygpath -w $PYTHONPATH)
. -
Vesanto about 8 yearsIf on windows 10, this is now: setx PYTHONHOME "C:\Python27" setx PYTHONPATH "C:\Python27\Lib" setx PATH "%PYTHONHOME%;%PATH%"
-
LM.Croisez over 7 yearsEnsure you don't have two Python version installed concurrently. This could mess the interpreter, if Python 2.7 is first in the %PATH%, and Python3 is set for PYTHONHOME, for example.
-
bubakazouba over 7 yearsdo i set both PYTHONPATH and PYTHONHOME or either or?
-
Sohan Shirodkar about 7 years@LM. Croisez I am currently having Python 2.7.5 and Python3.4.2 concurrently installed. For some reason I was forced to install the latter. I think it is this that is causing the problem. How can I uninstall the older version?
-
Jean-François Fabre over 6 yearsI would never use
setx PATH "%PYTHONHOME%;%PATH%"
because it sets userPATH
with the sum of user & system path. It's okay locally (usingset
) but not usingsetx
-
Thirst for Knowledge over 5 yearsThis worked for me fine on Windows 7 with Python 3.6
-
mt025 over 2 yearsIts missing... That would explain. Thanks
-
AmigoJack almost 2 yearsHowever, if only
site.pyo
exists (i.e. from a RenPy game) then one must also callpython -O
instead ofpython
only. No environment variable helps you with that.