根据Walker Hale IV对类似(但不同!;))问题的回答,这样做有两个关键:
- 你不需要安装distribute和pip,因为它们会自动包含在一个新的虚拟环境中(你可能只想要已经用virtualenv测试过的版本)
- 您可以使用 virtualenv 源代码来创建新的虚拟环境,而不是使用系统安装版本的 virtualenv
所以工作流程是:
- 在您的系统上安装 Python 版本 X
- 下载virtualenv源码版本Q(可能是最新的)
- 使用 Python X 和 virtualenv Q 创建新的虚拟环境
- 您的新 VE 现在正在运行 Python X 和最新的稳定版本的 pip 和分发
- pip 安装任何其他软件包
- easy_install 任何其他你不能 pip 安装的包
笔记:
- 在您的新虚拟环境中,您可以安装新(或旧)版本的分发、pip 或 virtualenv。(我认为)
- 我不使用 WH4 创建引导虚拟环境的技术。相反,我每次都从 virtualenv 源创建新的虚拟环境。
- 这种技术应该可以在任何操作系统上使用。
- 如果我要向对整个 Distribute/pip/virtualenv 生态系统概念不熟悉的人解释这一点,我会以以 virtualenv 为中心的方式进行解释。
我编写了一个 bash 脚本,它在 Ubuntu 中执行基本操作:
#! /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
输出看起来像这样(关闭下载并打开停用):
用户@计算机:/home/user$ 。env_create python3 /media/work/environments/test
/media/work/environments/test/bin/python3 中的新 python 可执行文件
还在 /media/work/environments/test/bin/python 中创建可执行文件
安装分发......完成。
安装 pip……完成。
蟒蛇 3.2
分发==0.6.19
wsgiref==0.1.2
用户@计算机:/媒体/工作/环境/测试$