8

私はUbuntuでPythonアプリケーションを開発しています。Distribute / virtualenv / pipエコシステムをセットアップして、PythonパッケージをシステムPythonパッケージ(Synapticで管理している、またはシステムに管理させている)とは独立して管理したいと考えています。

python-setuptools、python-virtualenv、およびpython-pipシステムパッケージをインストールして、順調に進むことができますが、Distribute、virtualenv、およびpipの最新/特定のバージョンを取得できるようにすることもできます。これらのPPAはないので、手動でインストールする必要があります。

最後の厄介な問題は、Pythonの複数のバージョンでこれを実行できるようにしたいということです。つまり、1つのエコシステムをpython2.6用に、別のエコシステムをpython用に、別のエコシステムをpython3用に、または64ビットシステム上に別のchrootされた32ビットPython用にセットアップします。

プロセスは次のようになると思います:

  • Python Xを使用して、Distributeの独自のコピーをホームフォルダー内の場所にインストールします
  • indie Distribute、easy_installpipを使用する
  • インディーピップを使用して、virtualenvをインストールします
  • インディーvirtualenvを使用して、仮想環境を作成します
  • 仮想環境をアクティブ化し、パッケージをインストールします
  • Python Y、Z、Qについて繰り返します

どのようなインストール/構成オプションを探していますか?

4

3 に答える 3

7

同様の(しかし明確な!;))質問に対する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 $
于 2011-07-29T15:04:19.787 に答える
0

@jfsebastianが指摘しているように、virtualenvwrapperはあなたが求めていることの多くまたはすべてを実行します。

http://virtualenvwrapper.readthedocs.org/

virtualenvwrapperは、IanBickingのvirtualenvツールの拡張機能のセットです。拡張機能には、仮想環境を作成および削除したり、開発ワークフローを管理したりするためのラッパーが含まれているため、依存関係に競合を生じさせることなく、一度に複数のプロジェクトで作業することが容易になります。

于 2013-10-23T20:46:24.700 に答える
0

JF Sebastianとnealmcbの貢献について詳しく説明しますが、最近は実際にシステムパッケージバージョンのvirtualenvwrapper(Ubuntu 12.04以降で利用可能)を使用しています。

virtualenvwrapperは、IanBickingのvirtualenvツールの拡張機能のセットです。拡張機能には、仮想環境を作成および削除したり、開発ワークフローを管理したりするためのラッパーが含まれているため、依存関係に競合を生じさせることなく、一度に複数のプロジェクトで作業することが容易になります。

私が(この質問に答えて)使用する主な機能は次のとおりです。

JFSで言及されている環境変数は、PIP_DOWNLOAD_CACHE、VIRTUALENV_USE_DISTRIBUTE、WORKON_HOME、VIRTUALENVWRAPPER_PYTHONをいじるのに実際に役立ちます。

virtualenv自体を更新する唯一の理由は、最新バージョンのsetuptools(以前はDistributeと呼ばれていましたが、以前はsetuptoolsと呼ばれていました)を入手することです。まだこれを行う必要はありませんが、新しいvirtualenvから始めて、最初にDistribute / setuptoolsをアップグレードし、次にpipをアップグレードしてから、他のライブラリをインストールするのが最も簡単だと思います。

新しいバージョンのvirtualenvが厳密に必要な場合は、ブートストラップスクリプトを変更する必要があります。

于 2013-10-24T10:14:32.660 に答える