"ImportError: No module named site" on Windows

298,443

Solution 1

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.

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

Solution 5

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

Share:
298,443

Related videos on Youtube

Mimminito
Author by

Mimminito

Android Mobile Applications Developer

Updated on July 08, 2022

Comments

  • Mimminito
    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 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?

    • Tim W.
      Tim W. over 12 years
      Assuming you didn't change the default install location, does the file C:/Python27/Lib/site.py exist on your computer?
    • Mimminito
      Mimminito over 12 years
      Yes, site.py is located in that folder
    • John Machin
      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
      CMCDragonkai over 10 years
      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.
    • robert
      robert about 9 years
      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.
    • idbrii
      idbrii about 9 years
      @robert: You should add that as an answer.
    • KJP
      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
    David Heffernan over 12 years
    python.org installer is just fine
  • Mimminito
    Mimminito over 12 years
    I 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
    Rishi about 12 years
    And you need to append %PYTHONHOME% to your path variable as well for this to work.
  • Timotei
    Timotei over 11 years
    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.
  • Piotr Dobrogost
    Piotr Dobrogost over 10 years
    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.
  • Damien
    Damien over 10 years
    Environment variables are a nightmare. I blame Linux.
  • Iain Samuel McLean Elder
    Iain Samuel McLean Elder about 10 years
    Use setx PYTHONHOME C:\Python27 to set this permanently for subsequent command prompts. Otherwise, you have to use the set command every time.
  • Kurt
    Kurt almost 10 years
    On Linux, try unsetting the pythonpath. unset PYTHONPATH or make sure that . is not in the PYTHONPATH.
  • robert
    robert about 9 years
    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).
  • Vesanto
    Vesanto about 8 years
    If on windows 10, this is now: setx PYTHONHOME "C:\Python27" setx PYTHONPATH "C:\Python27\Lib" setx PATH "%PYTHONHOME%;%PATH%"
  • LM.Croisez
    LM.Croisez over 7 years
    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.
  • bubakazouba
    bubakazouba over 7 years
    do i set both PYTHONPATH and PYTHONHOME or either or?
  • Sohan Shirodkar
    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
    Jean-François Fabre over 6 years
    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
  • Thirst for Knowledge
    Thirst for Knowledge over 5 years
    This worked for me fine on Windows 7 with Python 3.6
  • mt025
    mt025 over 2 years
    Its missing... That would explain. Thanks
  • AmigoJack
    AmigoJack almost 2 years
    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.