简体   繁体   English

未使用 pip 在 vi​​rtualenv 中安装 Python 包

[英]Python packages not installing in virtualenv using pip

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/gleenv/lib/python2.7/site-packages/pip-1.1-py2.7.egg 的 pip 1.1(python 2.7)

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.使用解释器 /usr/bin/python2.7 运行 virtualenv 在 glenv/bin/python2.7 中新的 python 可执行文件还在 glenv/bin/python 中创建可执行文件 安装分发...... ..................................................... ..................................................... ..................................................... .......................完毕。 Installing pip...............done.安装 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 升级):扭曲在/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 无法卸载要求 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 .下载/解压twisted 下载Twisted-12.3.0.tar.bz2 (2.6Mb):下载2.6Mb 运行setup.py egg_info 以获取twisted 包。 . . . .

Successfully installed twisted zope.interface distribute Cleaning up...成功安装了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. **当我尝试运行使用 twisted 的 Python 脚本时,我收到一条错误消息,指出未安装 twisted。 That is:那是:

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

The problem here is that you're using sudo when you shouldn't be. 这里的问题是您不应该使用sudo And that's causing pip to try to install into /usr/local/lib instead of ~/glenv/lib . 这导致pip尝试安装到/usr/local/lib而不是~/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 ,所以成功完成了此操作,但这对您没有帮助,因为您不允许在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. sudo pip可能导致此问题的原因有多种,但最可能的原因是:在大多数系统(包括我可以立即访问的各种Mac和RHEL / CentOS盒)上, sudoers文件将重置您的环境,然后重新添加在少数列入白名单的环境变量中。 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 ,它将看不到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). 需要注意的是,你想要删除的sudovirtualenv通话,因为这很可能导致VENV被错误地设置了(这就是为什么你需要的sudo chmod ,这不会是必要的,否则)。 The whole point of installing things under your user home directory is that you can do it with your normal user permissions. 在用户主目录下安装内容的全部目的是,您可以使用普通用户权限来进行安装。

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. 附带说明,您可能还想升级到较新的virtualenv / pip ,因为1.8和1.2进行了一些错误修复和改进。 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. 但是我验证了即使使用最新版本(1.8.4和1.2.1),我也遇到了与您完全相同的问题,因此我认为这与您无关。

The sudo pip is causing the problem here. sudo pip在这里引起了问题。 It will install the package in your system instead of the virtual environment you created.它会将软件包安装在您的系统中,而不是您创建的虚拟环境中。 So when it says the requirement is already satisfied.因此,当它说要求已经满足时。 Try to look add the directory it is pointing to.尝试添加它指向的目录。 Which is in your case, while you were trying to install, was /usr/local/lib/python2.7/dist-packages在您尝试安装时,您的情况是/usr/local/lib/python2.7/dist-packages

If it's inside something like /usr/local/lib/... , which does not points to your virtualenv folder then it is installed in your system.如果它在/usr/local/lib/... ,它不指向您的 virtualenv 文件夹,那么它将安装在您的系统中。 Otherwise, in correct scenario it will look something like this /usr/local/lib/..../<name of your virtualenv>/lib .否则,在正确的情况下,它看起来像这样/usr/local/lib/..../<name of your virtualenv>/lib

You can always use commands like which python & which pip to see if they both are using the placeholder of our virtual environment.您始终可以使用which pythonwhich pip类的命令来查看它们是否都在使用我们虚拟环境的占位符。

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

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