virtualenv不包括pip [英] virtualenv does not include pip

查看:105
本文介绍了virtualenv不包括pip的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Mac OS X El Capitan上使用virtualenv创建虚拟环境.我已经安装了带有brew的Python 2.7.11,默认情况下包括pipwheelsetuptools.

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:

    pip完成其工作后,
  1. virtualenv可执行文件未放置在/usr/local/bin中,因此我需要手动进行ln -s(这可能表示此步骤上的安装有问题).
  2. 运行virtualenv venv后,它将创建新环境,并从brew -installation中捕获Python 2.7.11,但是: bin文件夹中没有pip..这意味着,如果我尝试激活venvwhich pip,它将返回pip/usr/local/bin/pip的全局位置,而不是/path/to/venv/bin/pip.
  1. virtualenv executable is not placed in /usr/local/bin after pip makes its job, so I need to ln -s it by hand (it may indicate, that there is something wrong with installation on this step).
  2. After I run virtualenv venv, it creates new environment, catches Python 2.7.11 from brew-installation, but: there is no pip inside bin folder. That means, that if I try which pip, having venv activated, it returns a global position of pip/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:

  1. 我用$ virtualenv env
  2. 创建一个虚拟环境
  3. 使用$ source env/bin/activate
  4. 激活了虚拟环境
  5. 已检查的python位置:运行(env)$ which python,输出为/Users/<username>/env/bin/python(符合预期)
  6. 已检查的点位置:运行(env)$ which pip,输出为/usr/local/bin/pip(不需要)
  1. I create a virtualenvironment with $ virtualenv env
  2. Activated the virtual environment with $ source env/bin/activate
  3. Checked python location: run (env)$ which python with output /Users/<username>/env/bin/python (as expected)
  4. 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:

  1. 尝试安装软件包:(env)$ pip install HTTPServer成功
  2. 尝试运行该软件包:(env)$ python -m HTTPServer,该错误失败,错误为/Users/emunsing/env/bin/python: No module named HTTPServer
  3. 要仔细检查,请尝试再次安装:(env)$ pip install HTTPServer会产生Requirement already satisfied (use --upgrade to upgrade): HTTPServer in /usr/local/lib/python2.7/site-packages
  1. Try to install a package: (env)$ pip install HTTPServer which succeeds
  2. Try to run the package: (env)$ python -m HTTPServer which fails with error /Users/emunsing/env/bin/python: No module named HTTPServer
  3. To double-check, try to install again: (env)$ pip install HTTPServer which produces Requirement 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 activate.fish python python2 activate.csh activate_this.py python-config python2.7

$ ls env/bin activate activate.fish python python2 activate.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 upgradebrew 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,就像在以下类似问题中一样:此版本适用于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软件包安装到全局环境(例如$ pip install --user HTTPServer)时,您可能需要使用--user标志.如果您要在.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屋!

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