同様の(しかし明確な!;))質問に対するWalker Hale IVの回答に基づいて、これを行うには2つの鍵があります。
- これらは新しい仮想環境に自動的に含まれるため、distributeとpipをインストールする必要はありません(おそらく、virtualenvでテストされたバージョンのみが必要です)
- システムにインストールされたバージョンのvirtualenvを使用する代わりに、virtualenvソースコードを使用して新しい仮想環境を作成できます。
したがって、ワークフローは次のとおりです。
- システムにPythonバージョンXをインストールします
- virtualenvソースコードバージョンQ(おそらく最新)をダウンロードする
- PythonXとvirtualenvQを使用して新しい仮想環境を作成する
- 新しいVEは、PythonXと最新の安定したバージョンのpipおよびdistributeを実行しています。
- pipは他のパッケージをインストールします
- easy_installpipインストールできない他のパッケージ
ノート:
- 新しい仮想環境では、distribute、pip、またはvirtualenvの新しい(または古い)バージョンをインストールできます。(おもう)
- ブートストラップ仮想環境を作成するWH4の手法は使用しません。代わりに、毎回virtualenvソースから新しい仮想環境を作成します。
- この手法は、どのオペレーティングシステムでも使用できるはずです。
- これをDistribute/pip / virtualenvエコシステムの概念全体に不慣れな人に説明する場合は、virtualenv中心の方法で説明します。
Ubuntuの基本を実行するbashスクリプトを作成しました。
#! /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
出力は次のようになります(ダウンロードがオフになり、非アクティブ化がオンになります)。
user @ computer:/ home /user$。env_create python3 / media / work / environment / test
/ media / work / environment / test / bin/python3にある新しいPython実行可能ファイル
また、/ media / work / environment / test / bin/pythonに実行可能ファイルを作成します
配布のインストール...............完了。
pipのインストール...............完了。
Python 3.2
配布==0.6.19
wsgiref == 0.1.2
user @ computer:/ media / work / environment / test $