简体   繁体   English

virtualenv 不包括 pip

[英]virtualenv does not include pip

I am trying to create a virtual environment using virtualenv on Mac OS X El Capitan.我正在尝试在 Mac OS X El Capitan 上使用virtualenv创建一个虚拟环境。 I have installed Python 2.7.11 with brew , which includes pip , wheel and setuptools by default.我已经用brew安装了 Python 2.7.11 ,默认包括pipwheelsetuptools

Hovewer, when I try to install virtualenv following instructions in the documentation or from any other resource, I get several problems:然而,当我尝试按照文档或任何其他资源中的说明安装virtualenv时,我遇到了几个问题:

  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). virtualenv可执行文件在pip它的工作后没有放在/usr/local/bin中,所以我需要手动ln -s它(这可能表明,这一步安装有问题)。
  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 .在我运行virtualenv venv之后,它会创建新环境,从brew安装中捕获 Python 2.7.11,但是: bin文件夹中没有pip 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 .这意味着,如果我尝试which pip ,激活venv ,它会返回 pip 的全局pip/usr/local/bin/pip ,而不是/path/to/venv/bin/pip

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.因此,在venv内安装包使用全局pip并将它们安装到全局sites-packages ,而不是在venv内,这与环境应该做的完全相反。

Could you please suggest what may be wrong and how to fix it?您能否建议可能出了什么问题以及如何解决?

EDIT: 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 .编辑:值得一提的是,我以前在我的计算机上安装了其他版本的 Python,我最近已将其删除,如本答案中所述。 Maybe it causes the issue, and some more thorough cleaning is needed.也许它会导致问题,需要进行更彻底的清洁。

Try removing or renaming the .pydistutils.cfg file in your home directory, eg by renaming with mv ~/.pydistutils.cfg ~/oldpydistutils.cfg 尝试删除或重命名主目录中的.pydistutils.cfg文件,例如通过使用mv ~/.pydistutils.cfg ~/oldpydistutils.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! 如果您知道.pydistutils.cfg中的具体问题,请告诉我们!

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. 我遇到了同样的问题:我的虚拟环境是在没有pip的本地副本的情况下创建的,尽管它们有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. 这意味着在安装到全局包位置的虚拟环境中使用$ pip ,并且环境的python不可见。

How I diagnosed this on my machine: 我如何在我的机器上诊断出这个:

  1. I create a virtualenvironment with $ virtualenv env 我用$ virtualenv env创建了一个虚拟环境
  2. Activated the virtual environment with $ source env/bin/activate 使用$ source env/bin/activate激活虚拟环境
  3. Checked python location: run (env)$ which python with output /Users/<username>/env/bin/python (as expected) 检查python位置:run (env)$ which python with output /Users/<username>/env/bin/python (如预期的那样)
  4. Checked pip location: run (env)$ which pip with output /usr/local/bin/pip (NOT expected) 检查点位置: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. Try to install a package: (env)$ pip install HTTPServer which succeeds 尝试安装一个包: (env)$ pip install HTTPServer成功
  2. Try to run the package: (env)$ python -m HTTPServer which fails with error /Users/emunsing/env/bin/python: No module named HTTPServer 尝试运行包: (env)$ python -m HTTPServer失败,错误/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 要仔细检查,请尝试再次安装: (env)$ pip install HTTPServer ,它生成Requirement already satisfied (use --upgrade to upgrade): HTTPServer in /usr/local/lib/python2.7/site-packages

Double-checking, we see that there's no Pip in the environment's /bin folder: 仔细检查,我们发现环境的/ bin文件夹中没有Pip:

$ ls env/bin activate activate.fish python python2 activate.csh activate_this.py python-config python2.7

And so while the system finds the local python version, it can't find a local pip to use and traverses the $PATH. 因此,当系统找到本地python版本时,它找不到要使用的本地pip并遍历$ PATH。 It ended up using pip from /usr/local/bin, leaving me unable to install packages locally to the virtual environment. 它最终使用了来自/ usr / local / bin的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 这是我尝试的: - 重新安装python brew uninstall python然后进行brew upgradebrew install python --build-from-source - 使用如Pip文档中所述的get-pip.py命令安装pip

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 . 这是我排除的: - 我没有使用sudo pip ... 这在其他问题上引起了类似的问题,并且在这个Python / pip安装的任何时候都没有这样做 - 我的虚拟环境没有显示本地安装像这些类似问题中的情况一样: 这一个用于Windows这个用于Mac OS X.

Ultimately, I found that eliminating the ~/.pydistutils.cfg file fixed the problem, allowing for fresh virtual environments that had their own local pip. 最终,我发现消除~/.pydistutils.cfg文件解决了问题,允许新的虚拟环境拥有自己的本地点。 The contents of my ~/.pydistutils.cfg file were: 我的~/.pydistutils.cfg文件的内容是:

[global]
verbose=1

[install]
install-scripts=$HOME/bin

[easy_install]
install-scripts=$HOME/bin

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 . 简单地重命名~/.pydistutils.cfg文件似乎可以解决问题:虽然这个文件是由自制软件安装创建的,但是这个文件中的某些设置可能与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 (eg $ pip install --user HTTPServer ). 虽然删除此文件对我的系统没有任何不良影响,但在将pip包安装到全局环境时(例如$ pip install --user HTTPServer ),可能需要使用--user标志。 Here are more details on .pydistutils.cfg if you want to work on tailoring it for your needs. 有关.pydistutils.cfg的更多详细信息,如果您想根据自己的需要定制它。

I had the issue when running virtualenv: "ImportError: No module named pip." 我在运行virtualenv时遇到了问题:“ImportError:没有名为pip的模块。” My solution was to downgrade virtualenv. 我的解决方案是降级virtualenv。 I had 16.2.0. 我有16.2.0。 pip uninstall virtualenv pip install virtualenv==15.1.0 pip uninstall virtualenv pip install virtualenv == 15.1.0

  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). pip其工作之后, virtualenv可执行文件没有放在/usr/local/bin ,所以我需要手动ln -s (它可能表明在这一步安装时出现了问题)。

Don't do that. 不要那样做。 That will only hide the bug and not solve the problem. 这只会隐藏错误而不能解决问题。 Here's a short guide how to debug this kind of issues: 以下是如何调试此类问题的简短指南:

  • Start with which -a python . which -a python开始。 The first path you see should be /usr/local/bin/python , if not check your PATH variable. 您看到的第一个路径应该是/usr/local/bin/python ,如果没有检查您的PATH变量。

  • Next, check which -a pip . 接下来,检查which -a pip Again the first path should be /usr/local/bin/pip . 第一条路径应该是/usr/local/bin/pip If not, run python -m ensurepip and recheck. 如果没有,运行python -m ensurepip并重新检查。

  • Now install virtualenv using pip install virtualenv , after that check the output of which -a virtualenv . 现在安装virtualenv使用pip install virtualenv ,后检查的输出which -a virtualenv The first path should be /usr/local/bin/virtualenv , if not check the output of env |grep PYTHON for unexpected environment variables. 第一个路径应该是/usr/local/bin/virtualenv ,如果没有检查env |grep PYTHON的输出是否有意外的环境变量。

  • Finally check the output of virtualenv --version to make sure you have the latest version. 最后检查virtualenv --version的输出以确保您拥有最新版本。

Just hit same issue on Linux.刚在 Linux 上遇到同样的问题。 Seems like there are multiple causes of this issue, but for me answer was to remove ~/.pip/ .似乎这个问题有多种原因,但对我来说答案是删除~/.pip/

Details: I had this in my .pip/pip.conf for some reason I can't remember:详细信息:由于某些我不记得的原因,我在.pip/pip.conf中有这个:

$ cat ~/.pip/pip.conf 
[global]
use_https = True 
index = https://pypi.python.org/pypi
prefix = /home/sam/local/

and was using local versions on Python, Pip installed at ~/local/.并且在 Python 上使用本地版本,Pip 安装在 ~/local/. For some reason virtualenv installed pip must pick up prefix = /home/sam/local/ setting and pip was being installed there.由于某种原因, virtualenv安装的 pip 必须选择prefix = /home/sam/local/设置并且 pip 被安装在那里。

Try this: sudo apt install pythonV.v-distutils .试试这个: sudo apt install pythonV.v-distutils In my case Vv == 3.8.在我的例子中,Vv == 3.8。 This worked for me.这对我有用。

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

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