<p>基于<a href="https://stackoverflow.com/questions/4324558/whats-the-proper-way-to-install-pip-virtualenv-and-distribute-for-python/5177027#5177027">Walker Hale IV's answer</a>到一个相似的(但是不同的!;))问题,要做到这一点有两个关键:</p>
<ul>
<li>您不需要安装distribute和pip,因为它们会自动包含在一个新的虚拟环境中(而且您大概只希望使用virtualenv测试过的版本)</li>
<li>您可以使用virtualenv源代码创建新的虚拟环境,而不是使用系统安装的virtualenv版本</li>
</ul>
<p>所以工作流程是:</p>
<ul>
<li>在系统上安装pythonversionx</li>
<li>下载virtualenv源代码版本Q(可能是最新版本)</li>
<li>使用pythonx和virtualenvq创建新的虚拟环境</li>
<li>您的新VE现在正在运行pythonx和pip和distribute的最新稳定版本</li>
<li>pip安装任何其他软件包</li>
<li>easy_安装任何其他无法pip安装的软件包</li>
</ul>
<p>注意事项:</p>
<ul>
<li>在新的虚拟环境中,您可以安装distribute、pip或virtualenv的新(或旧)版本。(我想)</li>
<li>我不使用WH4的技术来创建引导虚拟环境。相反,我每次都从virtualenv源创建新的虚拟环境。在</li>
<li>这种技术应该适用于任何操作系统。在</li>
<li>如果我要向一个全新的Distribute/pip/virtualenv生态系统概念解释这一点,我将以virtualenv为中心进行解释。在</li>
</ul>
<p>我写了一个bash脚本来完成Ubuntu中的基本操作:</p>
<pre><code>
#! /bin/bash
# Script to create a python virtual environment
# independently of the system version of virtualenv
#
# Ideally this would be a cross-platform Python
# script with more validation and fewer TODOs,
# but you know how it is.
# = PARAMETERS =
# $1 is the python executable to use
# examples: python, python2.6, /path/to/python
# $2 is the new environment folder
# example: /path/to/env_folder/name
## TODO: should be just a name
## but I can't concatenate strings in bash
# $3 is a pip requirements file
# example: /path/to/req_folder/name.txt
# you must uncomment the relevant code below to use $3
## TODO: should be just a name
## but I can't concatenate strings in bash
# other parameters are hard-coded below
# and you must change them before first use
# = EXAMPLES OF USE =
# . env_create python2.5 /home/env/legacy
## creates environment "legacy" using python 2.5
# . env_create python /home/env/default
## creates environment "default" using whatever version of python is installed
# . env_create python3.2 /home/env/bleeding /home/req/testing.txt
## creates environment "bleeding" using python 3.2 and installs packages from testing.txt using pip
# = SET UP VARIABLES =
# Required version of virtualenv package
VERSION=1.6.4
# Folder to store your virtual environments
VE_FOLDER='/media/work/environments'
## TODO: not used because I can't concatenate strings in bash
# Folder to store bootstrap (source) versions of virtualenv
BOOTSTRAP_FOLDER='/media/work/environments/bootstrap'
## TODO: not used because I can't concatenate strings in bash
# Folder to store pip requirements files
REQUIREMENTS_FOLDER='/media/work/environments/requirements'
## TODO: not used because I can't concatenate strings in bash
# Base URL for downloading virtualenv source
URL_BASE=http://pypi.python.org/packages/source/v/virtualenv
# Universal environment options
ENV_OPTS=' no-site-packages distribute'
# $1 is the python interpreter
PYTHON=$1
# $2 is the target folder of the new virtual environment
VE_TARGET=$2
# $3 is the pip requirements file
REQ_TARGET=$3
## = DOWNLOAD VIRTUALENV SOURCE =
## I work offline so I already have this downloaded
## and I leave this bit commented out
# cd $BOOTSTRAP_DIR
# curl -O $URL_BASE/virtualenv-$VERSION.tar.gz
## or use wget
# = CREATE NEW ENV USING VIRTUALENV SOURCE =
cd $BOOTSTRAP_FOLDER
tar xzf virtualenv-$VERSION.tar.gz
# Create the environment
$PYTHON virtualenv-$VERSION/virtualenv.py $ENV_OPTS $VE_TARGET
# Don't need extracted version anymore
rm -rf virtualenv-$VERSION
# Activate new environment
cd $VE_TARGET
. bin/activate
# = INSTALL A PIP REQUIREMENTS FILE =
## uncomment this if you want to automatically install a file
# pip install -r $REQ_TARGET
# = REPORT ON THE NEW ENVIRONMENT =
python version
pip freeze
# deactivate
## uncomment this if you don't want to start in your environment immediately
</code></pre>
<p>输出如下所示(下载关闭,停用打开):</p>
^{pr2}$