为 Python 安装 pip、virtualenv 和分发的正确方法是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4324558/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 15:16:00  来源:igfitidea点击:

What's the proper way to install pip, virtualenv, and distribute for Python?

pythonvirtualenvsetuptoolsdistribute

提问by Matthew Rankin

Short Question

简短的问题

Background

背景

In my answerto SO question 4314376, I recommended using ez_setupso that you could then install pipand virtualenvas follows:

SO question 4314376 的回答中,我建议使用,ez_setup以便您可以安装pipvirtualenv如下所示:

curl -O http://peak.telecommunity.com/dist/ez_setup.py
sudo python ez_setup.py
sudo easy_install pip
sudo pip install virtualenv

I originally pulled these instructions from Jesse Noller's blog post So you want to use Python on the Mac?. I like the idea of keeping a clean global site-packages directory, so the only other packages I install there are virtualenvwrapperand distribute. (I recently added distributeto my toolbox because of this Python public service announcement. To install these two packages, I used:

我最初从 Jesse Noller 的博客文章中提取了这些说明So you want to use Python on the Mac? . 我喜欢保持一个干净的全局站点包目录的想法,所以我在那里安装的唯一其他包是 virtualenvwrapperdistribute. (我最近distribute因为这个 Python 公共服务公告而添加到我的工具箱中。为了安装这两个包,我使用了:

sudo pip install virtualenvwrapper
curl -O http://python-distribute.org/distribute_setup.py
sudo python distribute_setup.py

No more setuptools and easy_install

不再需要 setuptools 和 easy_install

To really follow that Python public service announcement, on a fresh Python install, I would do the following:

要真正遵循Python 公共服务公告,在全新安装 Python 时,我将执行以下操作:

curl -O http://python-distribute.org/distribute_setup.py
sudo python distribute_setup.py
sudo easy_install pip
sudo pip install virtualenv
sudo pip install virtualenvwrapper

Glyph's Rebuke

雕文的斥责

In a comment to my answerto SO question 4314376, SO user Glyphstated:

在对SO 问题 4314376 的回答的评论中,SO 用户Glyph表示:

NO. NEVER EVER do sudo python setup.py installwhatever. Write a ~/.pydistutils.cfg that puts your pip installation into ~/.local or something. Especially files named ez_setup.pytend to suck down newer versions of things like setuptools and easy_install, which can potentially break other things on your operating system.

不。永远不要做sudo python setup.py install任何事。编写一个 ~/.pydistutils.cfg 将您的 pip 安装放入 ~/.local 或其他东西。特别是命名的文件ez_setup.py往往会吸收更新版本的东西,比如 setuptools 和 easy_install,这可能会破坏操作系统上的其他东西。

Back to the short question

回到简短的问题

So Glyph's responseleads me to my original question:

所以Glyph 的回答让我想到了我最初的问题:

回答by Vinay Sajip

I think Glyph means do something like this:

我认为 Glyph 的意思是做这样的事情:

  1. Create a directory ~/.local, if it doesn't already exist.
  2. In your ~/.bashrc, ensure that ~/.local/binis on PATHand that ~/.localis on PYTHONPATH.
  3. Create a file ~/.pydistutils.cfgwhich contains

    [install]
    prefix=~/.local
    

    It's a standard ConfigParser-format file.

  4. Downloaddistribute_setup.pyand run python distribute_setup.py(no sudo). If it complains about a non-existing site-packagesdirectory, create it manually:

    mkdir -p ~/.local/lib/python2.7/site-packages/

  5. Run which easy_installto verify that it's coming from ~/.local/bin

  6. Run pip install virtualenv
  7. Run pip install virtualenvwrapper
  8. Create a virtual env containing folder, say ~/.virtualenvs
  9. In ~/.bashrcadd

    export WORKON_HOME
    source ~/.local/bin/virtualenvwrapper.sh
    
  1. 创建一个目录~/.local,如果它不存在。
  2. 在你的~/.bashrc,确保~/.local/bin打开PATH~/.local打开PYTHONPATH
  3. 创建一个文件~/.pydistutils.cfg,其中包含

    [install]
    prefix=~/.local
    

    这是一个标准的 ConfigParser 格式文件。

  4. 下载distribute_setup.py并运行python distribute_setup.py(否sudo)。如果它抱怨一个不存在的site-packages目录,请手动创建它:

    mkdir -p ~/.local/lib/python2.7/site-packages/

  5. 运行which easy_install以验证它来自~/.local/bin

  6. pip install virtualenv
  7. pip install virtualenvwrapper
  8. 创建一个包含文件夹的虚拟环境,比如 ~/.virtualenvs
  9. ~/.bashrc

    export WORKON_HOME
    source ~/.local/bin/virtualenvwrapper.sh
    

That's it, no use of sudoat all and your Python environment is in ~/.local, completely separate from the OS's Python. Disclaimer: Not sure how compatible virtualenvwrapperis in this scenario - I couldn't test it on my system :-)

就是这样,根本没有使用,sudo并且您的 Python 环境在 中~/.local,与操作系统的 Python 完全分开。免责声明:不确定virtualenvwrapper在这种情况下的兼容性如何- 我无法在我的系统上测试它:-)

回答by Alexis Métaireau

There is no problem to do sudo python setup.py install, if you're sure it's what you want to do.

如果您确定这是您想要做的,那么执行 sudo python setup.py install 没有问题。

The difference is that it will use the site-packages directory of your OS as a destination for .py files to be copied.

不同之处在于它将使用您操作系统的 site-packages 目录作为要复制的 .py 文件的目的地。

so, if you want pip to be accessible os wide, that's probably the way to go. I do not say that others way are bad, but this is probably fair enough.

因此,如果您希望 pip 可以在整个操作系统范围内访问,那可能就是要走的路。我不是说其他​​方式不好,但这可能已经足够公平了。

回答by Walker Hale IV

You can do this without installing anythinginto python itself.

你可以在不安装任何东西到 python 本身的情况下做到这一点。

You don't need sudo or any privileges.

您不需要 sudo 或任何特权。

You don't need to edit any files.

您不需要编辑任何文件。

Install virtualenv into a bootstrap virtual environment. Use the that virtual environment to create more. Since virtualenv ships with pip and distribute, you get everything from one install.

将 virtualenv 安装到引导虚拟环境中。使用那个虚拟环境来创建更多。由于 virtualenv 附带 pip 和分发版,因此您只需一次安装即可获得所有内容。

  1. Download virtualenv:
  2. Unpack the source tarball
  3. Use the unpacked tarball to create a clean virtual environment. This virtual environment will be used to "bootstrap" others. All of your virtual environments will automatically contain pip and distribute.
  4. Using pip, install virtualenv into that bootstrap environment.
  5. Use that bootstrap environment to create more!
  1. 下载虚拟环境:
  2. 解压源 tarball
  3. 使用解压后的 tarball 创建一个干净的虚拟环境。这个虚拟环境将用于“引导”其他人。您所有的虚拟环境都将自动包含 pip 和分发。
  4. 使用 pip,将 virtualenv 安装到该引导环境中。
  5. 使用该引导环境来创建更多!

Here is an example in bash:

这是 bash 中的一个示例:

# Select current version of virtualenv:
VERSION=12.0.7
# Name your first "bootstrap" environment:
INITIAL_ENV=bootstrap
# Set to whatever python interpreter you want for your first environment:
PYTHON=$(which python)
URL_BASE=https://pypi.python.org/packages/source/v/virtualenv

# --- Real work starts here ---
curl -O $URL_BASE/virtualenv-$VERSION.tar.gz
tar xzf virtualenv-$VERSION.tar.gz
# Create the first "bootstrap" environment.
$PYTHON virtualenv-$VERSION/virtualenv.py $INITIAL_ENV
# Don't need this anymore.
rm -rf virtualenv-$VERSION
# Install virtualenv into the environment.
$INITIAL_ENV/bin/pip install virtualenv-$VERSION.tar.gz

Now you can use your "bootstrap" environment to create more:

现在您可以使用您的“引导程序”环境来创建更多:

# Create a second environment from the first:
$INITIAL_ENV/bin/virtualenv py-env1
# Create more:
$INITIAL_ENV/bin/virtualenv py-env2

Go nuts!

发疯!

Note

笔记

This assumes you are not using a really old version of virtualenv. Old versions required the flags --no-site-packges(and depending on the version of Python, --distribute). Now you can create your bootstrap environment with just python virtualenv.py path-to-bootstrapor python3 virtualenv.py path-to-bootstrap.

这假设您没有使用真正旧版本的 virtualenv。旧版本需要标志--no-site-packges(取决于 Python 的版本,--distribute)。现在,您可以仅使用python virtualenv.py path-to-bootstrap或来创建引导环境python3 virtualenv.py path-to-bootstrap

回答by Sridhar Ratnakumar

Install ActivePython. It includes pip, virtualenv and Distribute.

安装 ActivePython。它包括 pip、virtualenv 和 Distribute。

回答by éric Araujo

I came across the same problem recently. I'm becoming more partial to the “always use a virtualenv” mindset, so my problem was to install virtualenv with pip without installing distribute to my global or user site-packages directory. To do this, I manually downloaded distribute, pip and virtualenv, and for each one I ran “python setup.py install --prefix ~/.local/python-private” (with a temporary setting of PYTHONPATH=~/.local/python-private) so that setup scripts were able to find distribute). I've moved the virtualenv script to another directory I have on my PATH and edited it so that the distribute and virtualenv modules can be found on sys.path. Tada: I did not install anything to /usr, /usr/local or my user site-packages dir, but I can run virtualenv anywhere, and in that virtualenv I get pip.

我最近遇到了同样的问题。我越来越偏向于“总是使用 virtualenv”的心态,所以我的问题是使用 pip 安装 virtualenv,而不安装分发到我的全局或用户站点包目录。为此,我手动下载了distribute、pip 和virtualenv,并为每个我运行了“python setup.py install --prefix ~/.local/python-private”(临时设置为 PYTHONPATH=~/.local/ python-private),以便安装脚本能够找到分发)。我已将 virtualenv 脚本移动到我的 PATH 上的另一个目录并对其进行了编辑,以便可以在 sys.path 上找到分发和 virtualenv 模块。Tada:我没有在 /usr、/usr/local 或我的用户站点包目录中安装任何东西,但是我可以在任何地方运行 virtualenv,并且在那个 virtualenv 中我得到了 pip。

回答by metakermit

If you follow the steps advised in several tutorials I linked in this answer, you can get the desired effect without the somewhat complicated "manual" steps in Walker's and Vinay's answers. If you're on Ubuntu:

如果您按照我在此答案中链接的几个教程中建议的步骤进行操作,则无需 Walker 和 Vinay 的答案中有些复杂的“手动”步骤即可获得所需的效果。如果您使用的是 Ubuntu:

sudo apt-get install python-pip python-dev

The equivalent is achieved in OS X by using homebrew to install python (more details here).

在 OS X 中通过使用 homebrew 安装 python 来实现等价物(更多细节在这里)。

brew install python

With pipinstalled, you can use it to get the remaining packages (you can omit sudoin OS X, as you're using your local python installation).

随着pip安装,你可以用它来获得剩余的包(可以省略sudo在OS X,如您使用的Python的安装)。

sudo pip install virtualenvwrapper

(these are the only packages you need installed globally and I doubt that it will clash with anything system-level from the OS. If you want to be super-safe, you can keep the distro's versions sudo apt-get install virtualenvwrapper)

(这些是您需要全局安装的唯一软件包,我怀疑它会与操作系统中的任何系统级别发生冲突。如果您想超级安全,可以保留发行版的版本sudo apt-get install virtualenvwrapper

Note:in Ubuntu 14.04 I receive some errors with pip install, so I use pip3 install virtualenv virtualenvwrapperand add VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3to my .bashrc/.zshrcfile.

注意:在 Ubuntu 14.04 中,我收到了一些 pip install 错误,所以我使用pip3 install virtualenv virtualenvwrapper并添加VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3到我的.bashrc/.zshrc文件中。

You then append to your .bashrcfile

然后你附加到你的.bashrc文件

export WORKON_HOME
source /usr/local/bin/virtualenvwrapper.sh

and source it

并来源

. ~/.bashrc

This is basically it. Now the only decision is whether you want to create a virtualenv to include system-level packages

基本上就是这样。现在唯一的决定是您是否要创建一个 virtualenv 以包含系统级包

mkvirtualenv --system-site-packages foo

where your existing system packages don't have to be reinstalled, they are symlinked to the system interpreter's versions. Note:you can still install new packages and upgrade existing included-from-system packages without sudo - I tested it and it works without any disruptions of the system interpreter.

在不必重新安装现有系统包的情况下,它们被符号链接到系统解释器的版本。注意:您仍然可以在没有 sudo 的情况下安装新软件包并升级现有的包含系统软件包 - 我测试了它并且它可以在不中断系统解释器的情况下工作。

kermit@hocus-pocus:~$ sudo apt-get install python-pandas
kermit@hocus-pocus:~$ mkvirtualenv --system-site-packages s
(s)kermit@hocus-pocus:~$ pip install --upgrade pandas
(s)kermit@hocus-pocus:~$ python -c "import pandas; print(pandas.__version__)"
0.10.1
(s)kermit@hocus-pocus:~$ deactivate
kermit@hocus-pocus:~$ python -c "import pandas; print(pandas.__version__)"
0.8.0

The alternative, if you want a completely separated environment, is

如果您想要一个完全分离的环境,另一种选择是

mkvirtualenv --no-site-packages bar

or given that this is the default option, simply

或者鉴于这是默认选项,只需

mkvirtualenv bar

The result is that you have a new virtualenv where you can freely and sudolessly install your favourite packages

结果是您拥有了一个新的 virtualenv,您可以在其中自由且无需 sudoless 安装您喜欢的软件包

pip install flask

回答by bukzor

I made this procedure for us to use at work.

我制作了这个程序供我们在工作中使用。

cd ~
curl -s https://pypi.python.org/packages/source/p/pip/pip-1.3.1.tar.gz | tar xvz
cd pip-1.3.1
python setup.py install --user
cd ~
rm -rf pip-1.3.1

$HOME/.local/bin/pip install --user --upgrade pip distribute virtualenvwrapper

# Might want these three in your .bashrc
export PATH=$PATH:$HOME/.local/bin
export VIRTUALENVWRAPPER_VIRTUALENV_ARGS="--distribute"
source $HOME/.local/bin/virtualenvwrapper.sh

mkvirtualenv mypy
workon mypy
pip install --upgrade distribute
pip install pudb # Or whatever other nice package you might want.

Key points for the security minded:

安全意识的关键点:

  1. curl does ssl validation. wget doesn't.
  2. Starting from pip 1.3.1, pip also does ssl validation.
  3. Fewer users can upload the pypi tarball than a github tarball.
  1. curl 进行 ssl 验证。wget 没有。
  2. 从 pip 1.3.1 开始,pip 也做 ssl 验证。
  3. 与 github tarball 相比,可以上传 pypi tarball 的用户更少。

回答by Victor S

There are good instructions on the Virtualenv official site. https://pypi.python.org/pypi/virtualenv

Virtualenv 官方网站上有很好的说明。https://pypi.python.org/pypi/virtualenv

Basically what I did, is install pipwith sudo easy_install pip, then used sudo pip install virtualenvthen created an environment with: virtualenv my_env(name it what you want), following that I did: virtualenv --distribute my_env; which installed distributeand pipin my virtualenv.

基本上我做什么,是安装pipsudo easy_install pip,然后用于sudo pip install virtualenv再创建一个环境:virtualenv my_env(命名为你想要的),下面是我所做的:virtualenv --distribute my_env; 其安装distributepip在我的virtualenv。

Again, follow the instruction on the virtualenvpage.

再次按照virtualenv页面上的说明进行操作。

Kind of a hassle, coming from Ruby ;P

有点麻烦,来自 Ruby ;P

回答by Arik Halperin

On Ubuntu:

在 Ubuntu 上:

sudo apt-get install python-virtualenv

sudo apt-get install python-virtualenv

The package python-pipis a dependency, so it will be installed as well.

该包python-pip是一个依赖项,因此它也将被安装。

回答by kioopi

Update:As of July 2013 this project is no longer maintained. The author suggests using pyenv. (pyenv does not have built-in support for virtualenv, but plays nice with it.)

更新:截至 2013 年 7 月,该项目不再维护。作者建议使用pyenv。(pyenv 没有对 virtualenv 的内置支持,但可以很好地使用它。)

Pythonbrewis a version manager for python and comes with support for virtualenv.

Pythonbrew是 Python 的版本管理器,并支持 virtualenv。

After installing pythonbrew and a python-version using venvs is really easy:

使用 venvs 安装 pythonbrew 和 python 版本后真的很容易:

# Initializes the virtualenv 
pythonbrew venv init

# Create a virtual/sandboxed environment 
pythonbrew venv create mycoolbundle  

# Use it 
pythonbrew venv use mycoolbundle