短的问题

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

背景

在我对SO问题4314376的回答中,我建议使用ez_setup,这样你就可以像下面这样安装pip和virtualenv:

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

我最初从Jesse Noller的博客文章中提取了这些说明所以你想在Mac上使用Python ?我喜欢保持一个干净的全局site-packages目录的想法,所以我在那里安装的其他包只有virtualenvwrapper和distribute。(因为这个Python公共服务公告,我最近把分发添加到我的工具箱中。为了安装这两个包,我使用了:

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

不再有setuptools和easy_install

为了真正遵循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

字形的责备

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

不。永远不要使用sudo python setup.py安装任何东西。编写~/.pydistutils.cfg文件,将pip安装放在~/目录下。本地的或者其他的。特别是名为ez_setup.py的文件往往会占用setuptools和easy_install等新版本的东西,这可能会破坏操作系统上的其他东西。

回到那个简短的问题

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

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


当前回答

好消息是,如果您已经安装了python3.4,那么pyvenv已经安装了。所以,就

pyvenv project_dir
source project_dir/bin/activate
python --version   
python 3.4.*

现在,在这个虚拟环境中,您可以使用pip为这个项目安装模块。

离开这个虚拟的环境

deactivate

其他回答

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

Create a directory ~/.local, if it doesn't already exist. In your ~/.bashrc, ensure that ~/.local/bin is on PATH and that ~/.local is on PYTHONPATH. Create a file ~/.pydistutils.cfg which contains [install] prefix=~/.local It's a standard ConfigParser-format file. Download distribute_setup.py and run python distribute_setup.py (no sudo). If it complains about a non-existing site-packages directory, create it manually: mkdir -p ~/.local/lib/python2.7/site-packages/ Run which easy_install to verify that it's coming from ~/.local/bin Run pip install virtualenv Run pip install virtualenvwrapper Create a virtual env containing folder, say ~/.virtualenvs In ~/.bashrc add export WORKON_HOME source ~/.local/bin/virtualenvwrapper.sh

就是这样,根本不用sudo,你的Python环境在~/中。本地,完全独立于操作系统的Python。免责声明:不确定在这种情况下virtualenvwrapper的兼容性如何-我无法在我的系统上测试它:-)

我做了这个程序,在工作中使用。

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.

安全意识的要点:

Curl执行SSL验证。wget没有。 从pip 1.3.1开始,pip还进行ssl验证。 可以上传pypi tarball的用户比上传github tarball的用户少。

Python 3.4开始

Python 3.3增加了venv模块,Python 3.4增加了ensurepip模块。这使得bootstrapping像下面这样简单:

Python -m ensurepip

可能在虚拟环境中调用venv来执行此操作。

PEP 453中描述了保证pip。

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.

如果你遵循我在这个答案中链接的几个教程中建议的步骤,你就会 就能得到想要的效果,而不需要沃克和维奈回答中有些复杂的“手动”步骤。如果你用的是Ubuntu:

sudo apt-get install python-pip python-dev

在OS X中,通过使用自制程序安装python可以实现相同的功能(更多细节请点击此处)。

brew install python

安装pip后,您可以使用它来获取剩余的包(在OS X中可以省略sudo,因为您正在使用本地python安装)。

sudo pip install virtualenvwrapper

(这些是你唯一需要在全球安装的包,我怀疑它会与任何系统级的操作系统发生冲突。如果你想要超级安全,你可以保留发行版的版本

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

然后追加到.bashrc文件

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

来源

. ~/.bashrc

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

mkvirtualenv --system-site-packages foo

现有的系统包不需要重新安装,它们被符号链接到系统解释器的版本。注意:你仍然可以在没有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

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

mkvirtualenv --no-site-packages bar

或者假设这是默认选项,简单地

mkvirtualenv bar

结果是,您有了一个新的virtualenv,在那里您可以自由而缓慢地安装您最喜欢的软件包

pip install flask