virtualenv 不包括 pip [英] virtualenv does not include pip
问题描述
我正在尝试在 Mac OS X El Capitan 上使用 virtualenv
创建一个虚拟环境.我已经安装了带有 brew
的 Python 2.7.11,默认情况下包括 pip
、wheel
和 setuptools
.
I am trying to create a virtual environment using virtualenv
on Mac OS X El Capitan. I have installed Python 2.7.11 with brew
, which includes pip
, wheel
and setuptools
by default.
然而,当我尝试按照 virtualenv 时>文档或任何其他资源,我遇到了几个问题:
Hovewer, when I try to install virtualenv
following instructions in the documentation or from any other resource, I get several problems:
virtualenv
可执行文件在pip
完成工作后没有放在/usr/local/bin
中,所以我需要ln-s
手动(可能表示这一步安装有问题).- 在我运行
virtualenv venv
后,它创建了新环境,从brew
-installation 捕获 Python 2.7.11,但是:没有pip
在bin
文件夹内.这意味着,如果我尝试which pip
,激活venv
,它会返回pip
的全局位置 —/usr/local/bin/pip
,而不是/path/to/venv/bin/pip
.
virtualenv
executable is not placed in/usr/local/bin
afterpip
makes its job, so I need toln -s
it by hand (it may indicate, that there is something wrong with installation on this step).- After I run
virtualenv venv
, it creates new environment, catches Python 2.7.11 frombrew
-installation, but: there is nopip
insidebin
folder. That means, that if I trywhich pip
, havingvenv
activated, it returns a global position ofpip
—/usr/local/bin/pip
, not/path/to/venv/bin/pip
.
因此,在 venv
内安装包使用全局 pip
并将它们安装到全局 sites-packages
,而不是在 内>venv
,这与环境应该做的完全相反.
As a consequence, installing packages inside venv
uses global pip
and installs them to a global sites-packages
, not that inside venv
, and it's quite the opposite of what environment should do.
能否请您建议可能出现的问题以及如何解决?
Could you please suggest what may be wrong and how to fix it?
值得一提的是,我曾经在我的计算机上安装了其他版本的 Python,我最近删除了它,因为它被描述为 在这个答案中.也许它会导致问题,需要更彻底的清洁.
The thing to mention is that I used to have other versions of Python installed on my computer, which I have recently deleted as it is described in this answer. Maybe it causes the issue, and some more thorough cleaning is needed.
推荐答案
尝试删除或重命名主目录中的 .pydistutils.cfg 文件,例如通过使用 mv ~/.pydistutils.cfg ~/oldpydistutils.cfg
Try removing or renaming the .pydistutils.cfg file in your home directory, e.g. by renaming with mv ~/.pydistutils.cfg ~/oldpydistutils.cfg
我在这里提供了一个详细的答案来帮助其他人,但最初的功劳归于这个答案.如果您知道 .pydistutils.cfg 中具体是什么导致了问题,请告诉我!
I'm putting a detailed answer here to help others, but the original credit goes to this answer. If you know what specifically in .pydistutils.cfg was causing the problem, let me know!
我遇到了同样的问题:我的虚拟环境是在没有 pip 的本地副本的情况下创建的,尽管他们有 python 的本地副本.这意味着从安装到全局包位置的虚拟环境中使用 $pip
并且对环境的 python 不可见.
I was having the same issue: my virtual environments were created without a local copy of pip, although they had a local copy of python. This meant that using $ pip
from within the virtual environment installed to the global package location, and was not visible to the environment's python.
我是如何在我的机器上诊断的:
How I diagnosed this on my machine:
- 我用
$ virtualenv env
创建了一个虚拟环境 - 使用
$ source env/bin/activate
激活虚拟环境 - 检查 python 位置:运行
(env)$ which python
输出/Users/
(如预期)/env/bin/python - 检查 pip 位置:运行
(env)$ which pip
输出/usr/local/bin/pip
(不需要)
- I create a virtualenvironment with
$ virtualenv env
- Activated the virtual environment with
$ source env/bin/activate
- Checked python location: run
(env)$ which python
with output/Users/<username>/env/bin/python
(as expected) - Checked pip location: run
(env)$ which pip
with output/usr/local/bin/pip
(NOT expected)
要检查我们的包去哪里,我们可以尝试在虚拟环境中安装一个包:
To check where our packages are going, we can try to install a package in the virtual environment:
- 尝试安装一个包:
(env)$ pip install HTTPServer
成功 - 尝试运行包:
(env)$ python -m HTTPServer
失败并出现错误/Users/emunsing/env/bin/python: No module named HTTPServer
- 再次检查,再次尝试安装:
(env)$ pip install HTTPServer
产生Requirement already meet (use --upgrade to upgrade): HTTPServer in/usr/local/lib/python2.7/site-packages
- Try to install a package:
(env)$ pip install HTTPServer
which succeeds - Try to run the package:
(env)$ python -m HTTPServer
which fails with error/Users/emunsing/env/bin/python: No module named HTTPServer
- To double-check, try to install again:
(env)$ pip install HTTPServer
which producesRequirement already satisfied (use --upgrade to upgrade): HTTPServer in /usr/local/lib/python2.7/site-packages
再次检查,我们看到环境的/bin文件夹中没有Pip:
Double-checking, we see that there's no Pip in the environment's /bin folder:
$ ls env/bin激活 activate.fish python python2activate.csh activate_this.py python-config python2.7
因此当系统找到本地python版本时,它找不到本地pip使用并遍历$PATH.它最终使用了/usr/local/bin 中的 pip,让我无法在本地将软件包安装到虚拟环境中.
And so while the system finds the local python version, it can't find a local pip to use and traverses the $PATH. It ended up using pip from /usr/local/bin, leaving me unable to install packages locally to the virtual environment.
这是我尝试过的:- 重新安装 python brew uninstall python
然后是 brew upgrade
和 brew install python --build-from-source
- 安装 pip 使用 get-pip.py 命令,如 Pip 文档中所述
Here's what I tried:
- Reinstalling python brew uninstall python
followed by brew upgrade
and brew install python --build-from-source
- Installing pip using the get-pip.py command as described in the Pip documentation
这是我排除的:- 我没有使用 sudo pip ...
在另一个问题中引起了类似的问题,并且在此 Python/pip 安装中从未这样做过- 我的虚拟环境没有显示 pip 的本地安装,就像这些类似问题中的情况:Windows 的这个,这个适用于 Mac OS X.
Here's what I ruled out:
- I was not using sudo pip ...
which caused similar problems in this other question and haven't done so at any time on this Python/pip install
- My virtual environment didn't show a local installation of pip, as was the case in these similar questions: This one for Windows, This one for Mac OS X.
最终,我发现删除 ~/.pydistutils.cfg
文件解决了这个问题,允许拥有自己的本地 pip 的全新虚拟环境.我的 ~/.pydistutils.cfg
文件的内容是:
Ultimately, I found that eliminating the ~/.pydistutils.cfg
file fixed the problem, allowing for fresh virtual environments that had their own local pip. The contents of my ~/.pydistutils.cfg
file were:
[global]
verbose=1
[install]
install-scripts=$HOME/bin
[easy_install]
install-scripts=$HOME/bin
简单地重命名 ~/.pydistutils.cfg
文件似乎可以解决这个问题:看起来虽然这个文件是由自制软件安装创建的,此文件中的某些设置可能与 virtualenv 不兼容.虽然删除此文件对我的系统没有任何不良影响,但在使用 pip 将软件包安装到全局环境时,您可能需要使用 --user
标志(例如 $ pip install --user HTTPServer
).如果您想了解有关 .pydistutils.cfg 的更多详细信息努力根据您的需求定制它.
Simply renaming the ~/.pydistutils.cfg
file appears to fix the problem: it seems that although this file was created by the homebrew installation, some settings in this file may be incompatible with virtualenv. While removing this file hasn't had any bad effects on my system, you may need to use the --user
flag when installing packages with pip to the global environment (e.g. $ pip install --user HTTPServer
). Here are more details on .pydistutils.cfg if you want to work on tailoring it for your needs.
这篇关于virtualenv 不包括 pip的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!