107

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 floats

When 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?

AmigoJack
  • 5,234
  • 1
  • 15
  • 31
Mimminito
  • 2,803
  • 3
  • 21
  • 27
  • Assuming you didn't change the default install location, does the file C:/Python27/Lib/site.py exist on your computer? – Tim W. Apr 08 '11 at 20:18
  • Yes, site.py is located in that folder – Mimminito Apr 10 '11 at 08:53
  • 8
    @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. – John Machin Jan 05 '12 at 20:28
  • I'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. – CMCDragonkai Jul 09 '13 at 11:02
  • 3
    I had this problem. I was invoking from Cygwin which had the unix-like path for `PYTHONPATH`. I converted it to a windows style path with `export 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. – robert Aug 19 '14 at 13:06
  • @robert: You should add that as an answer. – idbrii Oct 06 '14 at 22:46
  • @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. – KJP Oct 19 '17 at 03:04

17 Answers17

150

I've been looking into this problem for myself for almost a day and finally had a breakthrough. Try this:

  1. 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.

starball
  • 20,030
  • 7
  • 43
  • 238
RenderCase
  • 1,509
  • 1
  • 8
  • 2
  • 9
    And you need to append %PYTHONHOME% to your path variable as well for this to work. – Rishi Sep 20 '11 at 10:06
  • 7
    Aha! 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. – Timotei Mar 01 '12 at 15:07
  • 2
    Setting none of `PYTHONPATH/PYTHONHOME` variables should be necessary for Python to locate `site.py` module placed in the `Lib` folder. This answer is a workaround at best. – Piotr Dobrogost May 30 '13 at 18:59
  • 3
    Environment variables are a nightmare. I blame Linux. – Damien Aug 01 '13 at 00:48
  • On Linux, try unsetting the pythonpath. `unset PYTHONPATH` or make sure that `.` is not in the `PYTHONPATH`. – Kurt Feb 08 '14 at 18:37
  • 1
    Ensure 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. – LM.Croisez May 06 '16 at 13:45
  • 1
    do i set both PYTHONPATH and PYTHONHOME or either or? – bubakazouba May 26 '16 at 03:49
  • @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? – Sohan Shirodkar Sep 01 '16 at 16:09
44

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.

slckin
  • 937
  • 7
  • 12
12

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 exporting PYTHONHOME=/usr/ and PYTHONPATH=/usr/lib/python2.7

Jorge Vargas
  • 1,031
  • 11
  • 25
12

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

Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
Mika
  • 1,256
  • 13
  • 18
6

Locate site.py and add its path in PYTHONPATH. This will solve your problem.

DigviJay Patil
  • 986
  • 14
  • 31
  • Its missing... That would explain. Thanks – mt025 Jun 05 '21 at 08:58
  • However, if only `site.pyo` exists (i.e. from a RenPy game) then one must also call `python -O` instead of `python` only. No environment variable helps you with that. – AmigoJack Dec 14 '21 at 09:19
5

In my case, the issue was another site.py file, that was resolved earlier than the one from Python\Lib, due to PATH setting.

Environment: Windows 10 Pro, Python27.

My desktop has pgAdmin installed, which has file C:\Program Files (x86)\pgAdmin\venv\Lib\site.py. Because PATH environment variable had pdAdmin's home earlier than Python (apparently a bad idea in the first place), pgAdmin's site.py was found first.

All I had to do to fix the issue was to move pgAdmin's home later than Python, in PATH

Ivan C
  • 123
  • 2
  • 5
5

Are you trying to run Windows Python from Cygwin? I'm having the same problem. Python in Cygwin fails to import site. Python in Cmd works.

It looks like you need to make sure you run PYTHONHOME and PYTHONPATH through cygwin -aw to make them Windows paths. Also, python seems to be using some incorrect paths.

I think I'll need to install python through cygwin to get it working.

idbrii
  • 10,975
  • 5
  • 66
  • 107
  • 1
    Hi 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 with `export PYTHONPATH=$(cygpath -w $PYTHONPATH)`. – robert Aug 19 '14 at 13:13
4

For Windows 10 (follow up on @slckin answer), this can be set through the command line with:

setx PYTHONHOME "C:\Python27"
setx PYTHONPATH "C:\Python27\Lib"
setx PATH "%PYTHONHOME%;%PATH%"
Vesanto
  • 516
  • 11
  • 19
  • 7
    I would _never_ use `setx PATH "%PYTHONHOME%;%PATH%"` because it sets user `PATH` with the sum of user & system path. It's okay locally (using `set`) but not using `setx` – Jean-François Fabre Aug 07 '17 at 21:43
3

For me it happened because I had 2 versions of python installed - python 27 and python 3.3. Both these folder had path variable set, and hence there was this issue. To fix, this, I moved python27 to temp folder, as I was ok with python 3.3. So do check environment variables like PATH,PYTHONHOME as it may be a issue. Thanks.

kinshuk4
  • 3,241
  • 3
  • 33
  • 41
1

If somebody will find that it's still not working under non-admin users:

Example error:

ImportError: No module named iso8601

you need to set '--always-unzip' option for easy_install:

easy_install --always-unzip python-keystoneclient

It will unzip your egg files and will allow import to find em.

Noma4i
  • 751
  • 7
  • 19
1

I went through the same issue of ImportError: No module named site while installing python 2.7.11

Initially I had Python2.5 and the PYTHONHOME path was set to Python2.5. I renamed it to C:\Python27\ and it resolved the problem.

Debashish
  • 1,155
  • 19
  • 34
0

I up voted slckin's answer. My problem was that I was thoughtful and added double quotes around the paths. I removed the double quotes in all of the three variables: PYTHONHOME, PYTHONPATH, and PATH. Note that this was in a cmd or bat file to setup the environment for other tools. However, the double quotes may be useful in an icon setting. Typing

set

revealed that the quotes where in the path and not dropped as expected. I also shorted the PATH so that it was less than 256 characters long.

Greg
  • 233
  • 2
  • 12
0

You may try the Open Source Active Python Setup which is a well done Python installer for Windows. You just have to desinstall your version and install it...

philnext
  • 3,242
  • 5
  • 39
  • 62
0

First uninstall python and again install the latest version during installation use custom install and mark all user checkbox and set the installation path C:\Python 3.9 and make PYTHON_HOME value C:\Python 3.9 in the Environmental variable it works for me

-1

I have an application which relies heavily on Python and have kept up-to-date with python 2.7.x as new versions are released. Everthing has been fine until 2.7.11 when I got the same "No module named site" error. I've set PYTHONHOME to c:\Python27 and it's working. But the mystery remains why this is now needed when it wasn't with previous releases. And, if it is needed, why doesn't the installer set this var?

Dave Lawrence
  • 1,281
  • 12
  • 17
-1

I had the same problem. My solution was to repair the Python installation. (It was a new installation so I did not expect a problem but now it is solved.)

To repair (Windows 7):

  1. go to Control Panel -> Programs -> Programs and Features
  2. click on the Python version installed and then press Uninstall/Change.
  3. follow the instructions to repair the installation.
-3

Install yaml from the PyYAML home pagee: http://www.pyyaml.org/wiki/PyYAML

Select the appropriate version for your OS and Python.