Mac - Python - 导入错误:“没有名为站点的模块”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25968239/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 23:53:33  来源:igfitidea点击:

Mac - Python - import error: "No module named site"

pythonmacospippython-requestsimporterror

提问by user3728456

Tonight I am trying to get the package called "requests" installed and have begun fumbling around with the terminal and do not have very much intuition when it comes to this sort of thing.

今晚我正在尝试安装名为“requests”的包,并开始在终端上摸索,并且对这类事情没有太多直觉。

Computer is a mac mini, osx version 10.9.4

电脑是mac mini,osx版本10.9.4

In /Library/Python I have 4 folders: 2.3 2.5 2.6 and 2.7.
In /Applications I have "Python 2.7" and "Python 3.4"
I can open IDLE and type 8+8 and I get 16 just fine.

在 /Library/Python 我有 4 个文件夹:2.3 2.5 2.6 和 2.7。
在 /Applications 中,我有“Python 2.7”和“Python 3.4”,
我可以打开 IDLE 并输入 8+8,我得到 16 就好了。

Here is the error I am getting in terminal:

这是我在终端中遇到的错误:

 host-210-117:~ Mario$ python       
 ImportError: No module named site       

 host-210-117:~ Mario$ pip       
 ImportError: No module named site      

My goal is to run this command in the terminal:

我的目标是在终端中运行此命令:

 pip install requests

I believe pip is already installed. I run the file "get-pip.py" in IDLE and this is what it says:

我相信 pip 已经安装。我在 IDLE 中运行文件“get-pip.py”,它是这样说的:

 Requirement already up-to-date: pip in /Library/Python/2.7/site-packages/pip-1.5.6-py2.7.egg
 Cleaning up...
 Requirement already up-to-date: pip in /Library/Python/2.7/site-packages/pip-1.5.6-py2.7.egg
 Cleaning up...

This may have something to do with paths? I would appreciate some guidance/hints/tips, thanks!

这可能与路径有关?我将不胜感激一些指导/提示/技巧,谢谢!

Oh and just a little more info that might help solve this question. Here is the first few lines of the program I'm running:

哦,还有一些可能有助于解决这个问题的信息。这是我正在运行的程序的前几行:

 import base64
 import hmac
 import json
 import requests
 import time
 import urllib
 import os

Which gives me this error in IDLE (so I guess it importing those first few packages with no problem?) :

这在 IDLE 中给了我这个错误(所以我猜它导入前几个包没有问题?):

 >>> 

 Traceback (most recent call last):
   File "/Users/Mario/Desktop/pyak/pyak.py", line 4, in <module>
     import requests
 ImportError: No module named requests
 >>> 

***** System path list:

***** 系统路径列表:

sys.path ['/Users/Mario/Desktop/pyak', '/Users/Mario/Documents', '/Library/Python/2.7/site-packages/pip-1.5.6-py2.7.egg', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages', '/Library/Python/2.7/site-packages']

sys.path ['/Users/Mario/Desktop/pyak', '/Users/Mario/Documents', '/Library/Python/2.7/site-packages/pip-1.5.6-py2.7.egg', ' /Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7', '/Library/Frameworks/Python.framework /Versions/2.7/lib/python2.7/plat-darwin', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac', '/Library/Frameworks/Python.framework /Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk', '/Library/Frameworks /Python.framework/Versions/2.7/lib/python2.7/lib-old', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages', '/Library/Python/2.7/site-packages']

*** another update:

*** 另一个更新:

 host-210-117:~ Mario$ which python      
 /Library/Frameworks/Python.framework/Versions/2.7/bin/python

*** a little more info (is this supposed to happen?)

*** 更多信息(这是应该发生的吗?)

 host-210-117:~ Mario$ which pip       
 /usr/local/bin/pip        
 host-210-117:~ Mario$ pip       
 ImportError: No module named site       

*** After changing .bashrc

*** 更改 .bashrc 后

.bashrc: here's what in the file

.bashrc:这是文件中的内容

export PATH="$PATH:$HOME/.rvm/bin" # Add RVM to PATH for scripting
alias python=/Library/Python/2.7/python

export PATH="$PATH:$HOME/.rvm/bin" # 添加 RVM 到 PATH 脚本
别名 python=/Library/Python/2.7/python

here is the output of running:

这是运行的输出:

  pip install requests      

host-210-117:~ Mario$ pip install requests
Downloading/unpacking requests
Downloading requests-2.4.1-py2.py3-none-any.whl (458kB): 458kB downloaded
Installing collected packages: requests
Cleaning up...
Exception:
Traceback (most recent call last):
File "/Library/Python/2.7/site-packages/pip-1.5.6-py2.7.egg/pip/basecommand.py", line 122, in main status = self.run(options, args)
File "/Library/Python/2.7/site-packages/pip-1.5.6-py2.7.egg/pip/commands/install.py", line 283, in run requirement_set.install(install_options, global_options, root=options.root_path)
File "/Library/Python/2.7/site-packages/pip-1.5.6-py2.7.egg/pip/req.py", line 1435, in install requirement.install(install_options, global_options, *args, **kwargs)
File "/Library/Python/2.7/site-packages/pip-1.5.6-py2.7.egg/pip/req.py", line 671, in install self.move_wheel_files(self.source_dir, root=root) File "/Library/Python/2.7/site-packages/pip-1.5.6-py2.7.egg/pip/req.py", line 901, in move_wheel_files pycompile=self.pycompile,
File "/Library/Python/2.7/site-packages/pip-1.5.6-py2.7.egg/pip/wheel.py", line 215, in move_wheel_files clobber(source, lib_dir, True)
File "/Library/Python/2.7/site-packages/pip-1.5.6-py2.7.egg/pip/wheel.py", line 205, in clobber os.makedirs(destdir)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/os.py", line 157, in makedirs mkdir(name, mode)
OSError: [Errno 13] Permission denied: '/Library/Python/2.7/site-packages/requests'

host-210-117:~ Mario$ pip install requests
下载/解包请求
下载 requests-2.4.1-py2.py3-none-any.whl (458kB):458kB 下载
安装收集的包:请求
清理...
例外:
回溯(最近一次调用):
文件“/Library/Python/2.7/site-packages/pip-1.5.6-py2.7.egg/pip/basecommand.py”,第122行,主状态= self.run (选项,参数)
文件“/Library/Python/2.7/site-packages/pip-1.5.6-py2.7.egg/pip/commands/install.py”,第283行,运行requirements_set.install(install_options, global_options, root=options.root_path)
文件“/Library/Python/2.7/site-packages/pip-1.5.6-py2.7.egg/pip/req.py”,第 1435 行,在 install requirements.install(install_options, global_options, *args, ** kwargs)
文件“/Library/Python/2.7/site-packages/pip-1.5.6-py2.7.egg/pip/req.py”,第 671 行,在安装 self.move_wheel_files(self.source_dir, root=root ) 文件“/Library/Python/2.7/site-packages/pip-1.5.6-py2.7.egg/pip/req.py”,第 901 行,在 move_wheel_files pycompile=self.pycompile,
文件“/Library/Python /2.7/site-packages/pip-1.5.6-py2.7.egg/pip/wheel.py", line 215, in move_wheel_files clobber(source, lib_dir, True)
File "/Library/Python/2.7/site-包/pip-1.5.6-py2.7.egg/pip/wheel.py", line 205, in clobber os.makedirs(destdir)
文件“/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/os.py”, line 157, in makedirs mkdir(name, mode)
OSError: [Errno 13] Permission denied: '/库/Python/2.7/site-packages/requests'

Storing debug log for failure in /Users/Mario/Library/Logs/pip.log

将失败的调试日志存储在 /Users/Mario/Library/Logs/pip.log

采纳答案by Burhan Khalid

From what I can tell you have three versions of Python on your system.

据我所知,您的系统上有三个版本的 Python。

  • The one that comes with OSX /Library/Frameworks/Python.framework/Versions/2.7/
  • Python 2.7 from python.org /Library/Python/2.7/site-packages
  • Python 3.4 from python.org
  • OSX 自带的那个 /Library/Frameworks/Python.framework/Versions/2.7/
  • 来自 python.org 的 Python 2.7 /Library/Python/2.7/site-packages
  • 来自 python.org 的 Python 3.4

pip is installed against the Python 2.7 version you downloaded (the one you see in your Applications folder), unfortunately the default Pythonfor your shell is the one that's bundled with OSX, and there is no pip installed there.

pip 是针对您下载的 Python 2.7 版本(您在 Applications 文件夹中看到的版本)安装的,不幸的是,您的 shell的默认 Python是与 OSX 捆绑在一起的那个,并且那里没有安装 pip。

IDLE is also bundled with the Python that you downloaded, which is why it keeps telling you that pip is installed, but it doesn't work from the shell.

IDLE 还与您下载的 Python 捆绑在一起,这就是为什么它一直告诉您 pip 已安装,但它在 shell 中不起作用。

Since you are probably using the Python downloaded from python.org as your "primary" Python (after all, its the one with IDLE that you are using), you need to set your shell environment to point to this Python as default.

由于您可能正在使用从 python.org 下载的 Python 作为您的“主要”Python(毕竟,它是您正在使用的具有 IDLE 的 Python),因此您需要将 shell 环境设置为默认指向此 Python。

The easiest way to do that is to add a variable in .bashrcthat creates an alias pythonand points it to the right binary. To do that, add this line to /Users/yourusername/.bashrc- files with .are hidden by default, so you'll have to write the entire file name in the command line to open it. Add the following line:

最简单的方法是添加一个变量.bashrc来创建别名python并将其指向正确的二进制文件。为此,将此行添加到/Users/yourusername/.bashrc-.默认情况下隐藏的文件,因此您必须在命令行中写入整个文件名才能打开它。添加以下行:

alias python=/Library/Python/2.7/python

Save the file and then close all terminal windows and open it again. Now type pipand it should work correctly, and then you can proceed to installing requests.

保存文件,然后关闭所有终端窗口并再次打开它。现在输入pip它应该可以正常工作,然后您可以继续安装请求。

For future reference, try to stick with one version of Python. I personally ignore the bundled version and use the one from brew, but you can stick to the Python downloaded from python.org.

为了将来参考,请尝试坚持使用一个版本的 Python。我个人忽略捆绑版本并使用来自 的版本brew,但您可以坚持使用从 python.org 下载的 Python。

回答by newtover

site.py is a standard module that is run by python by default. It allows tweaking sys.path and running some code before your code starts running. It should live in the standard library and can hardly be somehow absent. However, you can disable automatic importing of the module by passing -S switch to python.

site.py 是一个标准模块,默认由 python 运行。它允许在您的代码开始运行之前调整 sys.path 并运行一些代码。它应该存在于标准库中并且几乎不能以某种方式缺席。但是,您可以通过将 -S 开关传递给 python 来禁用模块的自动导入。

Anyway, you should somehow inspect why the module can not be imported. Try to examine sys.path list.

无论如何,您应该以某种方式检查为什么无法导入模块。尝试检查 sys.path 列表。

回答by Sanchay

You are trying to install the package in '/Library/Python/2.7/site-packages/requests' but it requires root permissions to do so. This should do the trick:

您正在尝试将软件包安装在“/Library/Python/2.7/site-packages/requests”中,但它需要 root 权限才能这样做。这应该可以解决问题:

$ sudo pip install requests

回答by Daantje

I fixed mine with:

我修复了我的:

brew reinstall python

brew reinstall python

It fixed all my broken paths. I think I broke it with a broken brew package that had a wrong python version dependancy or something like that.

它修复了我所有破碎的路径。我想我用一个损坏的 brew 包破坏了它,这个包有错误的 python 版本依赖或类似的东西。

回答by tenado

I met the same question, and error info is:

我遇到了同样的问题,错误信息是:

ModuleNotFoundError: No module named 'xxx'

and finally solved by

并最终解决了

brew install python3

brew link python3

sudo python3 -m pip install xxx
// or `sudo python3 -m pip install -r requirements.txt`

回答by Michael Van Rompaey

sudo easy_install pip
sudo pip install requests