问题:ImportError:Windows上没有模块命名的站点

我正在尝试首次安装Python。我从Python网站下载了以下安装程序: Python 2.7.1 Windows Installer(Windows二进制文件-不包括源代码)。然后,我运行安装程序,选择“所有用户”,一切都很好。我将Python安装到默认位置:

C:\Python27

接下来,要测试Python是否已正确安装,我导航到我的Python目录,并在Windows cmd提示符下运行了“ python”命令。它返回以下错误:

ImportError:没有名为站点的模块

当我执行“ python -v”时,我得到以下信息:

#installing zipimport hook
import zipimport # builtin

#ImportError: No module named site #installed zipimport钩子#clear 内置 ._
#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 #恢复sys.stdout
#restore sys.stderr #cleanup
#cleanup [1 ] zipimport #cleanup [1]信号
#cleanup[1 ] exceptions #cleanup [1] _warnings
#cleanup sys 内置的
#cleanup ints: 6 unfreed ints cleanup #cleanup浮动

当我执行dir时,C:\Python27\Lib\site.py*我得到以下信息:

C:\ Users \ Mimminito> dir C:\ Python27 \ Lib \ site.py *
驱动器C中的卷没有标签。
卷序列号是DAB9-A863

C:\ Python27 \ Lib目录

13/11/2010 20:08 20,389 site.py
1文件20,389字节
0目录694,910,976字节免费

有任何想法吗?

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 ran the “python” command in the windows cmd prompt. It returns me the following error:

ImportError: No module named site

When I do ‘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:

C:\Users\Mimminito>dir C:\Python27\Lib\site.py*
Volume in drive C has no label.
Volume Serial Number is DAB9-A863

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?


回答 0

我已经为自己研究了近一天的问题,终于取得了突破。试试这个:

  1. 设置PYTHONPATH / PYTHONHOME变量

    右键单击开始菜单中的“ 计算机”图标,然后转到属性。在左侧标签上,转到高级系统设置。在出现的窗口中,转到“ 高级”选项卡,然后在底部单击“ 环境变量”。单击用户变量列表,然后开始键入Python,然后重复“ 系统变量”,以确保您没有为PYTHONPATH或PYTHONHOME设置错误的变量。接下来,添加新变量(我也可以在System中而不是User中使用,尽管它也可能对User也适用):PYTHONPATH,设置为C:\ Python27 \ Lib。,设置为C:\ Python27PYTHONHOME

希望这可以帮助!

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!


回答 1

快速解决方案:设置PYTHONHOME和PYTHONPATH并在PATH上包含PYTHONHOME

例如,如果您安装到c:\ Python27

set PYTHONHOME=c:\Python27
set PYTHONPATH=c:\Python27\Lib
set PATH=%PYTHONHOME%;%PATH%

确保您在PYTHON *变量上没有结尾的’\’,这似乎会使它破坏。

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.


回答 2

在安装Windows Python和Cygwin Python并尝试从Cygwin运行Cygwin Python之后,出现了这个问题。我export通过PYTHONHOME = / usr /和PYTHONPATH = / usr / lib / python2.7 解决了它

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


回答 3

确保正确设置了PYTHONHOME环境变量。如果PYTHONHOME指向无效的位置或您要运行的其他Python安装,您将收到此错误。

试试这个:

C:\>set PYTHONHOME=C:\Python27
C:\>python

setx PYTHONHOME C:\Python27

将其永久设置为后续命令提示符

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


回答 4

找到site.py并将其路径添加到PYTHONPATH中。这样可以解决您的问题。

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


回答 5

您是否要从Cygwin运行Windows Python?我有同样的问题。Cygwin中的Python无法导入站点。Cmd中的Python可以工作。

看起来您需要确保通过以下方式运行PYTHONHOME和PYTHONPATH cygwin -aw来使它们成为Windows路径。另外,python似乎使用了一些不正确的路径。

我想我需要通过cygwin安装python才能正常工作。

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.


回答 6

对于Windows 10(按照@slckin回答),可以通过以下命令在命令行中进行设置:

setx PYTHONHOME "C:\Python27"
setx PYTHONPATH "C:\Python27\Lib"
setx PATH "%PYTHONHOME%;%PATH%"

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%"

回答 7

就我而言,问题是另一个site.py文件,由于PATH设置,它比Python \ Lib中的文件更早被解决。

环境:Windows 10 Pro,Python27。

我的桌面安装了pgAdmin,它具有文件C:\ Program Files(x86)\ pgAdmin \ venv \ Lib \ site.py。由于PATH环境变量比Python更早拥有pdAdmin的目录(显然这是一个坏主意),因此首先找到pgAdmin的site.py。

解决该问题所需要做的就是在PATH中将pgAdmin的主页移到比Python晚的地方

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


回答 8

对我而言,发生这种情况是因为我安装了2个版本的python-python 27和python 3.3。这两个文件夹都设置了路径变量,因此存在此问题。为了解决这个问题,我将python27移到了temp文件夹,因为我对python 3.3没问题。因此,请检查诸如PATH,PYTHONHOME之类的环境变量,因为这可能是一个问题。谢谢。

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.


回答 9

如果有人发现非管理员用户仍无法使用它:

错误示例:

ImportError: No module named iso8601

您需要为easy_install设置“ –always-unzip”选项:

easy_install --always-unzip python-keystoneclient

它将解压缩您的egg文件,并允许导入找到em。

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.


回答 10

ImportError: No module named site在安装python 2.7.11时遇到了同样的问题

最初我有Python2.5,PYTHONHOME路径设置为Python2.5。我将其重命名为,C:\Python27\并解决了该问题。

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.


回答 11

您可以尝试“ 开源Active Python设置”,它是Windows的出色Python安装程序。您只需要卸载并安装版本即可。

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…


回答 12

我对slckin的答案投了赞成票。我的问题是我考虑周全,并在路径周围加了双引号。我删除了所有三个变量的双引号:PYTHONHOME,PYTHONPATH和PATH。请注意,该文件位于cmd或bat文件中,用于设置其他工具的环境。但是,双引号可能在图标设置中很有用。打字

揭示了报价在路径中的位置而不是按预期下降。我还缩短了PATH,使其长度少于256个字符。

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.


回答 13

我有一个非常依赖Python的应用程序,并随着新版本的发布保持python 2.7.x的最新性。直到2.7.11,当我遇到相同的No module named site错误时,一切都很好。我已将PYTHONHOME设置为c:\Python27且正在运行。但是仍然有一个谜,为什么在以前的版本中不需要它,现在需要这样做。而且,如果需要,安装程序为什么不设置此变量?

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?


回答 14

我有同样的问题。我的解决方案是修复Python安装。(这是一个新安装,因此我没想到会出现问题,但现在已解决。)

要修复(Windows 7):

  1. 转到控制面板->程序->程序和功能
  2. 单击已安装的Python版本,然后按“卸载/更改”。
  3. 请按照说明修复安装。

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.

回答 15

从PyYAML主页安装yaml:http ://www.pyyaml.org/wiki/PyYAML

选择适合您的OS和Python的版本。

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

Select the appropriate version for your OS and Python.


声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。