简体   繁体   English

导入错误:Windows 上没有名为站点的模块

[英]ImportError: No module named site on Windows

I am trying to install Python for the first time.我是第一次尝试安装 Python。 I downloaded the following installer from the Python website: Python 2.7.1 Windows Installer (Windows binary -- does not include source) .我从 Python 网站下载了以下安装程序: Python 2.7.1 Windows Installer (Windows binary -- 不包括源代码) I then ran the installer, selected 'All Users' and all was fine.然后我运行安装程序,选择“所有用户”,一切都很好。 I installed Python into the default location:我将 Python 安装到默认位置:

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.接下来,为了测试 Python 是否安装正确,我导航到我的 Python 目录,并在 Windows cmd 提示符下运行“python”命令。 It returns me the following error:它返回以下错误:

ImportError: No module named site导入错误:没有名为站点的模块

When I do 'python -v' I get the following:当我执行 'python -v' 时,我得到以下信息:

#installing zipimport hook
import zipimport # builtin
#installing zipimport hook
import zipimport # builtin
#installed zipimport hook
#installing zipimport hook
import zipimport # builtin
#installed zipimport hook

#ImportError: No module named site #clear builtin ._ #ImportError: No module named site #clear builtin ._
#clear sys.path #clear sys.argv #clear sys.path #clear sys.argv
#clear sys.ps1 #clear sys.ps2 #clear sys.ps1 #clear sys.ps2
#clear sys.exitfunc #clear sys.exc_type #clear sys.exitfunc #clear sys.exc_type
#clear sys.exc_value #clear sys.exc_traceback #clear sys.exc_value #clear sys.exc_traceback
#clear sys.last_type #clear sys.last_value #clear sys.last_type #clear sys.last_value
#clear sys.last_traceback #clear sys.path_hooks #clear sys.last_traceback #clear sys.path_hooks
#clear sys.path_importer_cache #clear sys.meta_path #clear sys.path_importer_cache #clear sys.meta_path
#clear sys.flags #clear sys.float_info #clear sys.flags #clear sys.float_info
#restore sys.stdin #restore sys.stdout #restore sys.stdin #restore sys.stdout
#restore sys.stderr #cleanup main #restore sys.stderr #cleanup main
#cleanup [1 ] zipimport #cleanup [1 ] signal #cleanup [1 ] zipimport #cleanup [1 ] 信号
#cleanup [1 ] exceptions #cleanup [1 ] _warnings #cleanup [1 ] exceptions #cleanup [1 ] _warnings
#cleanup sys #cleanup builtin #cleanup sys #cleanup内置
#cleanup ints: 6 unfreed ints #cleanup floats #cleanup ints: 6 unfreed ints #cleanup 浮动

When I do dir C:\\Python27\\Lib\\site.py* I get the following:当我执行 dir C:\\Python27\\Lib\\site.py*我得到以下信息:

C:\\Users\\Mimminito>dir C:\\Python27\\Lib\\site.py* C:\\Users\\Mimminito>dir C:\\Python27\\Lib\\site.py*
Volume in drive C has no label.驱动器 C 中的卷没有标签。
Volume Serial Number is DAB9-A863卷序列号为 DAB9-A863

Directory of C:\\Python27\\Lib C:\\Python27\\Lib 目录

13/11/2010 20:08 20,389 site.py 13/11/2010 20:08 20,389 站点.py
1 File(s) 20,389 bytes 1 个文件 20,389 字节
0 Dir(s) 694,910,976 bytes free 0 目录 694,910,976 字节空闲

Any ideas?有任何想法吗?

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设置 PYTHONPATH / PYTHONHOME变量

    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 .在出现的窗口中,转到Advanced 选项卡,然后在底部单击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.单击用户变量列表并开始键入 Python,然后对System variables重复此操作,以确保您没有为 PYTHONPATH 或 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 .接下来,添加新变量(我在 System 而不是 User 中做的,虽然它也适用于 User ):PYTHONPATH,设置为C:\\Python27\\Lib PYTHONHOME , set to C:\\Python27 . PYTHONHOME ,设置为C:\\Python27

Hope this helps!希望这可以帮助!

Quick solution: set PYTHONHOME and PYTHONPATH and include PYTHONHOME on PATH快速解决方案:设置 PYTHONHOME 和 PYTHONPATH 并在 PATH 中包含 PYTHONHOME

For example if you installed to c:\\Python27例如,如果您安装到 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.确保 PYTHON* 变量上没有尾随 '\\',这似乎会破坏它。

I was having this issue after installing both Windows Python and Cygwin Python, and trying to run Cygwin Python from Cygwin.在安装 Windows Python 和 Cygwin Python 并尝试从 Cygwin 运行 Cygwin Python 后,我遇到了这个问题。 I solved it by export ing PYTHONHOME=/usr/ and PYTHONPATH=/usr/lib/python2.7我通过export PYTHONHOME=/usr/ 和 PYTHONPATH=/usr/lib/python2.7 解决了它

Make sure your PYTHONHOME environment variable is set correctly.确保您的 PYTHONHOME 环境变量设置正确。 You will receive this error if PYTHONHOME is pointing to invalid location or to another Python installation you are trying to run.如果 PYTHONHOME 指向无效位置或您尝试运行的另一个 Python 安装,您将收到此错误。

Try this:尝试这个:

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

Use

setx PYTHONHOME C:\Python27

to set this permanently for subsequent command prompts为后续的命令提示永久设置它

Locate site.py and add its path in PYTHONPATH .找到site.py并将其路径添加到PYTHONPATH 中 This will solve your problem.这将解决您的问题。

For Windows 10 (follow up on @slckin answer), this can be set through the command line with:对于 Windows 10(跟进@slckin 的回答),这可以通过命令行设置:

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

In my case, the issue was another site.py file, that was resolved earlier than the one from Python\\Lib, due to PATH setting.就我而言,问题是另一个 site.py 文件,由于 PATH 设置,该文件比 Python\\Lib 中的文件更早解决。

Environment: Windows 10 Pro, Python27.环境:Windows 10 Pro,Python27。

My desktop has pgAdmin installed, which has file C:\\Program Files (x86)\\pgAdmin\\venv\\Lib\\site.py.我的桌面安装了 pgAdmin,其中包含文件 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.因为 PATH 环境变量比 Python 早有 pdAdmin 的家(首先显然是个坏主意),所以首先找到了 pgAdmin 的 site.py。

All I had to do to fix the issue was to move pgAdmin's home later than Python, in PATH为了解决这个问题,我所要做的就是在 PATH 中比 Python 更晚地移动 pgAdmin 的家

Are you trying to run Windows Python from Cygwin?您是否正在尝试从 Cygwin 运行 Windows Python? I'm having the same problem.我有同样的问题。 Python in Cygwin fails to import site. Cygwin 中的 Python 无法导入站点。 Python in Cmd works. Cmd 中的 Python 有效。

It looks like you need to make sure you run PYTHONHOME and PYTHONPATH through cygwin -aw to make them Windows paths.看起来您需要确保通过cygwin -aw运行 PYTHONHOME 和 PYTHONPATH 以使它们成为 Windows 路径。 Also, python seems to be using some incorrect paths.此外,python 似乎使用了一些不正确的路径。

I think I'll need to install python through cygwin to get it working.我想我需要通过 cygwin 安装 python 才能让它工作。

For me it happened because I had 2 versions of python installed - python 27 and python 3.3.对我来说,这是因为我安装了 2 个版本的 python - python 27 和 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.为了解决这个问题,我将 python27 移到了临时文件夹,因为我对 python 3.3 没问题。 So do check environment variables like PATH,PYTHONHOME as it may be a issue.因此,请检查环境变量,如 PATH、PYTHONHOME,因为这可能是一个问题。 Thanks.谢谢。

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”选项:

easy_install --always-unzip python-keystoneclient

It will unzip your egg files and will allow import to find em.它将解压缩您的egg 文件并允许导入以找到它们。

I went through the same issue of ImportError: No module named site while installing python 2.7.11我遇到了同样的问题ImportError: No module named site while installation python 2.7.11

Initially I had Python2.5 and the PYTHONHOME path was set to Python2.5.最初我有 Python2.5 并且 PYTHONHOME 路径设置为 Python2.5。 I renamed it to C:\\Python27\\ and it resolved the problem.我将其重命名为C:\\Python27\\并解决了问题。

I up voted slckin's answer.我对 slckin 的回答投了赞成票。 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.我删除了所有三个变量中的双引号:PYTHONHOME、PYTHONPATH 和 PATH。 Note that this was in a cmd or bat file to setup the environment for other tools.请注意,这是在 cmd 或 bat 文件中为其他工具设置环境。 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.我还缩短了 PATH,使其长度少于 256 个字符。

You may try the Open Source Active Python Setup which is a well done Python installer for Windows.您可以尝试Open Source Active Python Setup ,它是 Windows 的一个很好的 Python 安装程序。 You just have to desinstall your version and install it...你只需要卸载你的版本并安装它......

首先卸载python并在安装过程中再次安装最新版本使用自定义安装并标记所有用户复选框并设置安装路径C:\\ Python 3.9并在环境变量中设置PYTHON_HOME值为C:\\ Python 3.9它对我有用

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.我有一个严重依赖 Python 的应用程序,并且随着新版本的发布与 Python 2.7.x 保持同步。 Everthing has been fine until 2.7.11 when I got the same " No module named site " error.一切都很好,直到 2.7.11 出现相同的“ No module named site ”错误。 I've set PYTHONHOME to c:\\Python27 and it's working.我已将 PYTHONHOME 设置为c:\\Python27并且它正在工作。 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?而且,如果需要,为什么安装程序不设置此变量?

I had the same problem.我有同样的问题。 My solution was to repair the Python installation.我的解决方案是修复 Python 安装。 (It was a new installation so I did not expect a problem but now it is solved.) (这是一个新安装,所以我没想到会出现问题,但现在已经解决了。)

To repair (Windows 7):修复(Windows 7):

  1. go to Control Panel -> Programs -> Programs and Features转到控制面板 -> 程序 -> 程序和功能
  2. click on the Python version installed and then press Uninstall/Change.单击安装的 Python 版本,然后按卸载/更改。
  3. follow the instructions to repair the installation.按照说明修复安装。

Install yaml from the PyYAML home pagee: http://www.pyyaml.org/wiki/PyYAML从 PyYAML 主页安装 yaml: http ://www.pyyaml.org/wiki/PyYAML

Select the appropriate version for your OS and Python.为您的操作系统和 Python 选择合适的版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM