在全局站点程序包而不是virtualenv中进行pip安装 [英] pip installing in global site-packages instead of virtualenv

查看:87
本文介绍了在全局站点程序包而不是virtualenv中进行pip安装的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用pip3将软件包安装在virtualenv中将导致该软件包安装在全局site-packages文件夹中,而不是安装在virtualenv文件夹中.这是我在OS X Mavericks(10.9.1)上设置Python3和virtualenv的方法:

Using pip3 to install a package in a virtualenv causes the package to be installed in the global site-packages folder instead of the one in the virtualenv folder. Here's how I set up Python3 and virtualenv on OS X Mavericks (10.9.1):

我使用Homebrew安装了Python3:

I installed Python3 using Homebrew:

ruby -e "$(curl -fsSL https://raw.github.com/Homebrew/homebrew/go/install)"
brew install python3 --with-brewed-openssl

更改了.bash_profile中的$PATH变量;添加了以下行:

Changed the $PATH variable in .bash_profile; added the following line:

export PATH=/usr/local/bin:$PATH

运行which python3返回/usr/local/bin/python3(在重新启动Shell之后).

Running which python3 returns /usr/local/bin/python3 (after restarting the shell).

注意:尽管which python3仍返回/usr/bin/python.

Note: which python3 still returns /usr/bin/python though.

使用pip3安装了virtualenv:

pip3 install virtualenv

下一步,创建一个新的virtualenv并将其激活:

Next, create a new virtualenv and activate it:

virtualenv testpy3 -p python3
cd testpy3
source bin/activate

注意:如果不指定-p python3,则virtualenv中bin文件夹中将缺少pip.

运行which pipwhich pip3均返回virtualenv文件夹:

Running which pip and which pip3 both return the virtualenv folder:

/Users/kristof/VirtualEnvs/testpy3/bin/pip3

现在,当我尝试安装例如在激活的virtualenv中使用pip进行Markdown,pip将安装在全局site-packages文件夹中,而不是virtualenv的site-packages文件夹中.

Now, when I try to install e.g. Markdown using pip in the activated virtualenv, pip will install in the global site-packages folder instead of the site-packages folder of the virtualenv.

pip install markdown

正在运行pip list返回:

Markdown (2.3.1)
pip (1.4.1)
setuptools (2.0.1)
virtualenv (1.11)

/Users/kristof/VirtualEnvs/testpy3/lib/python3.3/site-packages的内容:

__pycache__/
_markerlib/
easy_install.py
pip/
pip-1.5.dist-info/
pkg_resources.py
setuptools/
setuptools-2.0.2.dist-info/

/usr/local/lib/python3.3/site-packages的内容:

Markdown-2.3.1-py3.3.egg-info/
__pycache__/
easy-install.pth
markdown/
pip-1.4.1-py3.3.egg/
setuptools-2.0.1-py3.3.egg
setuptools.pth
virtualenv-1.11-py3.3.egg-info/
virtualenv.py
virtualenv_support/

如您所见,全局站点包文件夹包含Markdown,virtualenv文件夹不包含.

As you can see, the global site-packages folder contains Markdown, the virtualenv folder doesn't.

注意:我之前在不同的VM上安装了Python2和Python3(后跟

Note: I had Python2 and Python3 installed before on a different VM (followed these instructions) and had the same issue with Python3; installing packages in a Python2 based virtualenv worked flawlessly though.

任何提示,暗示……都将不胜感激.

Any tips, hints, … would be very much appreciated.

推荐答案

有趣的是您提出了这个问题,我只是遇到了完全相同的问题.我最终解决了它,但是我仍然不确定是什么原因造成的.

Funny you brought this up, I just had the exact same problem. I solved it eventually, but I'm still unsure as to what caused it.

尝试检查您的bin/pipbin/activate脚本.在bin/pip中,查看shebang.这是正确的吗?如果不是,请更正它.然后在bin/activate中的〜42行中,检查您的virtualenv路径是否正确.看起来像这样

Try checking your bin/pip and bin/activate scripts. In bin/pip, look at the shebang. Is it correct? If not, correct it. Then on line ~42 in your bin/activate, check to see if your virtualenv path is right. It'll look something like this

VIRTUAL_ENV="/Users/me/path/to/virtual/environment"

如果是错误的,请更正它,依次为deactivate. bin/activate,如果我们的共同问题有相同的原因,则它应该可以工作.如果仍然没有,无论如何,您都处在正确的轨道上.我经历了与您相同的问题解决例程,反复遍历which pip,遵循堆栈跟踪等等.

If it's wrong, correct it, deactivate, then . bin/activate, and if our mutual problem had the same cause, it should work. If it still doesn't, you're on the right track, anyway. I went through the same problem solving routine as you did, which piping over and over, following the stack trace, etc.

请务必确保

/Users/kristof/VirtualEnvs/testpy3/bin/pip3

是您想要的,而不是引用另一个类似名称的测试项目(我有这个问题,不知道它是如何开始的.我怀疑是同时运行多个virtualenvs).

is what you want, and not referring to another similarly-named test project (I had that problem, and have no idea how it started. My suspicion is running multiple virtualenvs at the same time).

如果这些都不起作用,则可能是一个临时解决方案,如乔·霍洛韦(Joe Holloway)所说,

If none of this works, a temporary solution may be to, as Joe Holloway said,

只需使用全路径运行virtualenv的pip(即,不依赖于搜索可执行路径),甚至不需要激活环境.它将做正确的事.

Just run the virtualenv's pip with its full path (i.e. don't rely on searching the executable path) and you don't even need to activate the environment. It will do the right thing.

也许不理想,但应该在紧要关头工作.

Perhaps not ideal, but it ought to work in a pinch.

链接到我的原始问题:

VirtualEnv/Pip尝试在全球范围内安装软件包

这篇关于在全局站点程序包而不是virtualenv中进行pip安装的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆