未使用 pip 在 virtualenv 中安装 Python 包
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14695278/
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
Python packages not installing in virtualenv using pip
提问by sav
I'm having trouble installing twisted
我在安装 Twisted 时遇到问题
pip --version
pip 1.1 from /home/chris/GL/GLBackend/glenv/lib/python2.7/site-packages/pip-1.1-py2.7.egg (python 2.7)
来自 /home/chris/GL/GLBackend/glenv/lib/python2.7/site-packages/pip-1.1-py2.7.egg (python 2.7) 的 pip 1.1
Create a virtual environment
创建虚拟环境
chris@chris-mint ~/GL/GLBackend $ sudo virtualenv -p python2.7 glenv
Running virtualenv with interpreter /usr/bin/python2.7 New python executable in glenv/bin/python2.7 Also creating executable in glenv/bin/python Installing distribute.............................................................................................................................................................................................done. Installing pip...............done.
使用解释器运行 virtualenv /usr/bin/python2.7 glenv/bin/python2.7 中的新 python 可执行文件也在 glenv/bin/python 中创建可执行文件安装分发.................. ………………………………………………………………………………………………………………………………………………………… ………………………………………………………………………………………………………………………………………………………… ………………………………………………………………………………………………………………………………………………………… .......................完毕。安装pip…………完成。
Just in case, I'll enable all permissions
以防万一,我将启用所有权限
chris@chris-mint ~/GL/GLBackend $ sudo chmod -R 777 glenv
chris@chris-mint ~/GL/GLBackend $ source glenv/bin/activate
(glenv)chris@chris-mint ~/GL/GLBackend $ pip freeze
argparse==1.2.1 distribute==0.6.24 wsgiref==0.1.2
argparse==1.2.1 分发==0.6.24 wsgiref==0.1.2
twisted is not listed here as installed
此处未列出 Twisted 已安装
(glenv)chris@chris-mint ~/GL/GLBackend $ sudo pip install twisted
Requirement already satisfied (use --upgrade to upgrade): twisted in /usr/local/lib/python2.7/dist-packages Requirement already satisfied (use --upgrade to upgrade): zope.interface>=3.6.0 in /usr/local/lib/python2.7/dist-packages (from twisted) Requirement already satisfied (use --upgrade to upgrade): distribute in /usr/local/lib/python2.7/dist-packages (from zope.interface>=3.6.0->twisted) Cleaning up... (glenv)chris@chris-mint ~/GL/GLBackend $ pip uninstall twisted Cannot uninstall requirement twisted, not installed Storing complete log in /home/chris/.pip/pip.log
需求已经满足(使用--upgrade升级):twisted in /usr/local/lib/python2.7/dist-packages 需求已经满足(使用--upgrade升级):zope.interface>=3.6.0 in / usr/local/lib/python2.7/dist-packages(来自twisted)需求已经满足(使用--upgrade升级):分发到/usr/local/lib/python2.7/dist-packages(来自zope.interface >=3.6.0->twisted) 正在清理... (glenv)chris@chris-mint ~/GL/GLBackend $ pip uninstall twisted 无法卸载要求扭曲,未安装 正在 /home/chris/.pip/ 中存储完整的日志日志文件
But when I install it it says that its already installed. Force the install:
但是当我安装它时,它说它已经安装了。强制安装:
sudo pip install -I twisted
Downloading/unpacking twisted Downloading Twisted-12.3.0.tar.bz2 (2.6Mb): 2.6Mb downloaded Running setup.py egg_info for package twisted . . .
Successfully installed twisted zope.interface distribute Cleaning up...
下载/解包twisted 下载Twisted-12.3.0.tar.bz2 (2.6Mb): 2.6Mb 下载运行setup.py egg_info 包twisted 。. .
成功安装twisted zope.interface 分发清理中...
And yet it still isn't installed
但它仍然没有安装
(glenv)chris@chris-mint ~/GL/GLBackend $ pip freeze
argparse==1.2.1 distribute==0.6.24 wsgiref==0.1.2
argparse==1.2.1 分发==0.6.24 wsgiref==0.1.2
**When I try running Python scripts which use twisted, I get an error saying that twisted is not installed. That is:
**当我尝试运行使用扭曲的 Python 脚本时,我收到一条错误消息,指出未安装扭曲。那是:
ImportError: No module named twisted.python**
导入错误:没有名为twisted.python 的模块**
采纳答案by abarnert
The problem here is that you're using sudowhen you shouldn't be. And that's causing pipto try to install into /usr/local/libinstead of ~/glenv/lib. (And, because you used sudo, it's successfully doing so, but that doesn't help you, because you're not allowing system site-packages in your venv.)
这里的问题是你在sudo不应该使用的时候使用。这导致pip尝试安装到/usr/local/lib而不是~/glenv/lib. (而且,因为您使用了sudo,所以它成功地这样做了,但这对您没有帮助,因为您不允许在您的 venv 中使用系统站点包。)
There are multiple reasons sudo pipcould lead to this behavior, but the most likely is this: On most systems (including the various Mac and RHEL/CentOS boxes I have immediate access to), the sudoersfile will reset your environment, then add back in a handful of whitelisted environment variables. This means that when you sudo pip, it will not see the environment variables that virtualenvsets up, so it will fall back to doing the default thing and install into your system Python, instead of your venv.
sudo pip导致这种行为的原因有多种,但最有可能的原因是:在大多数系统上(包括我可以立即访问的各种 Mac 和 RHEL/CentOS 机器),该sudoers文件将重置您的环境,然后重新添加一些列入白名单的环境变量。这意味着当你sudo pip,它不会看到设置的环境变量virtualenv,所以它会回退到做默认的事情并安装到你的系统 Python 中,而不是你的 venv。
But really, it doesn't matter whythis is happening. The answer is the same: just do pip installinstead of sudo pip install.
但实际上,为什么会发生这种情况并不重要。答案是一样的:只是做pip install而不是sudo pip install。
Note that you alsowant to remove the sudoon the virtualenvcall, as this will probably cause the venv to be set up incorrectly (which is why you need the sudo chmod, which wouldn't be necessary otherwise). The whole point of installing things under your user home directory is that you can do it with your normal user permissions.
请注意,您也想删除sudo的virtualenv通话,因为这很可能导致VENV被错误地设置了(这就是为什么你需要的sudo chmod,这不会是必要的,否则)。在您的用户主目录下安装东西的全部意义在于您可以使用普通用户权限进行安装。
As a side note, you also may want to upgrade to a newer virtualenv/pip, as 1.8 and 1.2 have some bug fixes and improvements. But I verified that I get exactly the same problem as you even with the latest (1.8.4 and 1.2.1) versions, so I don't think that's relevant here.
作为旁注,您可能还想升级到更新的virtualenv/ pip,因为 1.8 和 1.2 有一些错误修复和改进。但我证实,即使使用最新的(1.8.4 和 1.2.1)版本,我也遇到与您完全相同的问题,所以我认为这与此处无关。

