“导入错误:没有名为站点的模块”在 Windows 上
我第一次尝试安装Python。我从 Python 网站下载了以下安装程序: Python 2.7 .1 Windows Installer(Windows 二进制文件 - 不包括源代码)。然后我运行安装程序,选择“所有用户”,一切都很好。我将 Python 安装到默认位置:
C:\Python27
接下来,为了测试 Python 是否安装正确,我导航到 Python 目录,并在 Windows CMD 提示符下执行 python
。它返回给我以下错误:
导入错误:没有名为 site 的模块
当我执行 python -v 时,我得到以下信息:
#安装 zipimport 钩子
导入 zipimport # 内置
#安装了 zipimport 钩子
#ImportError:没有名为 site 的模块
#clear 内置._
#清除系统路径
#清除sys.argv
#清除sys.ps1
#清除sys.ps2
#清除sys.exitfunc
#清除sys.exc_type
#清除sys.exc_value
#清除 sys.exc_traceback
#清除sys.last_type
#清除sys.last_value
#清除 sys.last_traceback
#clear sys.path_hooks
#清除sys.path_importer_cache
#清除sys.meta_path
#清除系统标志
#清除sys.float_info
#恢复sys.stdin
#恢复sys.stdout
#恢复sys.stderr
#cleanup 主要
#cleanup [1] zipimport
#cleanup [1] 信号
#cleanup [1] 异常
#cleanup [1] _警告
#清理系统
#cleanup 内置
#cleanup 整数:6 个未释放的整数
#清理浮动
当我执行 dir C:\Python27\Lib\site.py*
时,我得到以下内容:
C:\Python27\Lib 目录 13/11/2010 20:08 20,389 网站.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 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(17)
对我来说,发生这种情况是因为我安装了 2 个版本的 python - python 27 和 python 3.3。这两个文件夹都设置了路径变量,因此出现了这个问题。为了解决这个问题,我将 python27 移动到临时文件夹,因为我可以使用 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.
如果有人发现它在非管理员用户下仍然无法工作:
示例错误:
您需要为 easy_install 设置“--always-unzip”选项:
它将解压缩您的 Egg 文件并允许导入以查找它们。
If somebody will find that it's still not working under non-admin users:
Example error:
you need to set '--always-unzip' option for easy_install:
It will unzip your egg files and will allow import to find em.
在安装 python 2.7.11 时,我遇到了同样的问题
ImportError: No module named site
最初我有 Python2.5,并且 PYTHONHOME 路径设置为 Python2.5。
我将其重命名为 C:\Python27\ 并解决了问题。
I went through the same issue of
ImportError: No module named site
while installing python 2.7.11Initially I had Python2.5 and the PYTHONHOME path was set to Python2.5.
I renamed it to
C:\Python27\
and it resolved the problem.您可以尝试开源 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...
我对 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
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.
首先卸载python并再次安装最新版本,在安装过程中使用自定义安装并标记所有用户复选框并设置安装路径C:\Python 3.9
并在环境变量中将 PYTHON_HOME 值设置为 C:\Python 3.9 它对我有用
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
我有一个严重依赖 Python 的应用程序,并随着新版本的发布而与 python 2.7.x 保持同步。一切都很好,直到 2.7.11,我收到了同样的“
没有名为 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 toc:\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?我也有同样的问题。我的解决方案是修复 Python 安装。 (这是一个新安装,所以我没想到会出现问题,但现在它已经解决了。)
要修复(Windows 7):
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):
从 PyYAML 主页安装 yaml:http://www.pyyaml.org/wiki/PyYAML
选择适合您的操作系统和 Python 的版本。
Install yaml from the PyYAML home pagee: http://www.pyyaml.org/wiki/PyYAML
Select the appropriate version for your OS and Python.
这个问题我自己研究了快一天了,终于有了突破性的进展。试试这个:
设置 PYTHONPATH / PYTHONHOME 变量
右键单击开始菜单中的计算机图标,转到属性。
在左侧选项卡上,转到“高级系统设置”。在出现的窗口中,转到“高级”选项卡,然后单击底部的“环境变量”。单击用户变量列表并开始输入 Python,然后重复输入“系统变量”,只是为了确保 PYTHONPATH 或 PYTHONHOME 的变量没有设置错误。接下来,添加新变量(我在系统中而不是用户中添加,尽管它也可能对用户有效):PYTHONPATH,设置为C:\Python27\Lib。
PYTHONHOME
,设置为C:\Python27。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.快速解决方案:设置 PYTHONHOME 和 PYTHONPATH 并在 PATH 上包含 PYTHONHOME
例如,如果您安装到 c:\Python27
确保 PYTHON* 变量上没有尾随“\”,这似乎也会破坏它。
Quick solution: set PYTHONHOME and PYTHONPATH and include PYTHONHOME on PATH
For example if you installed to c:\Python27
Make sure you don't have a trailing '\' on the PYTHON* vars, this seems to break it aswel.
我在安装 Windows Python 和 Cygwin Python 并尝试从 Cygwin 运行 Cygwin Python 后遇到了这个问题。我通过
export
ing 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
export
ing PYTHONHOME=/usr/ and PYTHONPATH=/usr/lib/python2.7确保您的 PYTHONHOME 环境变量设置正确。如果 PYTHONHOME 指向无效位置或您尝试运行的另一个 Python 安装,您将收到此错误。
试试这个:
用于
为后续命令提示永久设置此项
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:
Use
to set this permanently for subsequent command prompts
找到 site.py 并将其路径添加到 PYTHONPATH 中。这将解决您的问题。
Locate site.py and add its path in PYTHONPATH. This will solve your problem.
您是否尝试从 Cygwin 运行 Windows Python?我也有同样的问题。 Cygwin 中的 Python 无法导入站点。 Cmd 中的 Python 可以工作。
看起来您需要确保通过 cygwin -aw 运行 PYTHONHOME 和 PYTHONPATH 以使它们成为 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.
就我而言,问题是另一个 site.py 文件,由于 PATH 设置,该文件比 Python\Lib 中的文件更早得到解决。
环境:Windows 10 专业版、Python27。
我的桌面安装了 pgAdmin,其中包含文件 C:\Program Files (x86)\pgAdmin\venv\Lib\site.py。因为 PATH 环境变量的 pdAdmin 的 home 早于 Python(显然这首先是一个坏主意),所以首先找到 pgAdmin 的 site.py。
要解决这个问题,我所要做的就是将 pgAdmin 的 home 移到 PATH 中晚于 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
对于 Windows 10(跟进 @slckin 答案),可以通过命令行设置:
For Windows 10 (follow up on @slckin answer), this can be set through the command line with: