Python包没有使用pip在virtualenv中安装

时间:2021-01-11 18:38:30

I'm having trouble installing 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中创建可执行文件安装distribute ................ .................................................. .................................................. .................................................. .......................完成。安装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 distribute == 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/ pip.log

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 . . .

下载/拆包扭曲下载Twisted-12.3.0.tar.bz2(2.6Mb):2.6Mb下载运行setup.py egg_info for package twisted。 。 。

Successfully installed twisted zope.interface distribute Cleaning up...

成功安装twis​​ted 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 distribute == 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:

**当我尝试运行使用twisted的Python脚本时,我收到一条错误消息,指出未安装twis​​ted。那是:

ImportError: No module named twisted.python**

ImportError:没有名为twisted.python的模块**

1 个解决方案

#1


55  

The problem here is that you're using sudo when you shouldn't be. And that's causing pip to try to install into /usr/local/lib instead 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 pip could 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 sudoers file 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 virtualenv sets 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 why this is happening. The answer is the same: just do pip install instead of sudo pip install.

但实际上,为什么会发生这种情况并不重要。答案是一样的:只需要执行pip install而不是sudo pip install。

Note that you also want to remove the sudo on the virtualenv call, 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.

请注意,您还想删除virtualenv调用上的sudo,因为这可能会导致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)版本,我也会遇到完全相同的问题,因此我认为这与此无关。

#1


55  

The problem here is that you're using sudo when you shouldn't be. And that's causing pip to try to install into /usr/local/lib instead 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 pip could 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 sudoers file 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 virtualenv sets 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 why this is happening. The answer is the same: just do pip install instead of sudo pip install.

但实际上,为什么会发生这种情况并不重要。答案是一样的:只需要执行pip install而不是sudo pip install。

Note that you also want to remove the sudo on the virtualenv call, 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.

请注意,您还想删除virtualenv调用上的sudo,因为这可能会导致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)版本,我也会遇到完全相同的问题,因此我认为这与此无关。