基於Walker Hale IV's answer了類似(但不同的;!))的問題,有兩個關鍵這樣做:
- 你不需要安裝分發和點子,因爲這些都是自動包含在新的虛擬環境(你大概只需要那些已與virtualenv中測試的版本)
- 可以使用的virtualenv源代碼來創建一個新的虛擬環境,而不是使用的virtualenv
系統安裝的版本
所以工作流程是:
- 您的系統上安裝Python版本X
- 下載的virtualenv源代碼版本Q(可能是最新的)
- 創建使用Python X新的虛擬環境的virtualenv Q
- 您新的VE現在運行Python X和最新的穩定版本點和分發
- 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
輸出看起來像這樣(與下載關閉,停用開啓):
[email protected]:/home/user$ . env_create python3 /media/work/environments/test
New python executable in /media/work/environments/test/bin/python3
Also creating executable in /media/work/environments/test/bin/python
Installing distribute...............done.
Installing pip...............done.
Python 3.2
distribute==0.6.19
wsgiref==0.1.2
[email protected]:/media/work/environments/test$
這看起來像一個簡單的解決方案http://stackoverflow.com/questions/4324558/whats-the-proper-way-to-install-pip-virtualenv-and-distribute-for-python/5177027#5177027 – d3vid
目前使用virtualenvwrapper調查替代方法請參閱https://bitbucket.org/dhellmann/virtualenvwrapper/issue/105和https://bitbucket.org/dhellmann/virtualenvwrapper/issue/106 – d3vid