2011-07-25 32 views
8

我正在Ubuntu中開發Python應用程序。我想設置一個Distribute/virtualenv/pip ecosystem來獨立於任何系統Python包(我在Synaptic中管理,或者讓系統爲我管理它們)獨立管理我的Python包。如何在Ubuntu上正確安裝多個非包Distribute/virtualenv/pip生態系統?

我可以只安裝python-setuptools,python-virtualenv和python-pip系統軟件包,並以我的快樂方式,但我也希望能夠獲得Distribute,virtualenv和pip的最新/特定版本。這些沒有PPA,所以我必須手動安裝它們。

最後的複雜情況是,我希望能夠爲多個版本的Python做到這一點。也就是說,爲python2.6建立一個生態系統,爲python建立另一個生態系統,爲python3建立另一個生態系統,或爲另一個爲chrooted 32-bit Python建立另一個生態系統。

我猜這個過程會是這樣的:

  • 使用Python X安裝我自己分配到的位置拷貝在我的主文件夾
  • 使用獨立分配,easy_install的點子
  • 使用獨立點,安裝virtualenv
  • 使用獨立虛擬環境,創建虛擬環境
  • 激活虛擬環境,安裝軟件包
  • 對Python重複Y,Z和Q

我在找什麼安裝/配置選項?

+0

這看起來像一個簡單的解決方案http://stackoverflow.com/questions/4324558/whats-the-proper-way-to-install-pip-virtualenv-and-distribute-for-python/5177027#5177027 – d3vid

+0

目前使用virtualenvwrapper調查替代方法請參閱https://bitbucket.org/dhellmann/virtualenvwrapper/issue/105和https://bitbucket.org/dhellmann/virtualenvwrapper/issue/106 – d3vid

回答

0

在闡述JF塞巴斯蒂安和nealmcb的貢獻,這幾天我確實用我的系統virtualenvwrapper封裝版本(可在Ubuntu 12.04及更高版本)。

virtualenvwrapper是一組Ian Bicking的virtualenv工具的擴展。這些擴展包括用於創建和刪除虛擬環境的包裝器,以及用於管理開發工作流的包裝器,使得一次處理多個項目更容易,而不會在其依賴項中引入衝突。

我使用(在回答這個問題)的主要特點是:

  • mkvirtualenv --python=PYTHON_EXE創建使用特定的Python可執行程序(不必是系統封裝版本)
  • 一個的virtualenv
  • allvirtualenv pip install -U pip升級點子所有virtualenvs

的環境下上面提到的變量JFS對於擺弄:PIP_DOWNLOAD_CACHE,VIRTUALENV_USE_DISTRIBUTE,WORKON_HOME,VIRTUALENVWRAPPER_PYTHON確實很有用。

更新virtualenv本身的唯一原因是獲取最新版本的setuptools(以前稱爲Distribute,以前稱爲setuptools)。我還沒有必要這樣做,但我懷疑從新的virtualenv開始並首先升級Distribute/setuptools,然後升級pip,然後安裝其他庫最容易。

如果嚴格需要新版本的virtualenv,則應修改the bootstrap script

7

基於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$ 
+1

在Mac OSX Lion上正常工作,修復了以下錯誤:BOOTSTRAP_DIR應該是BOOTSTRAP_FOLDER。 – ohmi

+2

[virtualenvwrapper](http://virtualenvwrapper.readthedocs.org/en/latest/)簡化了管理多個虛擬環境,例如'workon','mkvirtualenv' /'rmvirtualenv'命令。另外我發現'PIP_DOWNLOAD_CACHE','VIRTUALENV_USE_DISTRIBUTE','WORKON_HOME','VIRTUALENVWRAPPER_PYTHON'環境變量很有用。 – jfs

+0

關於bash中的字符串連接,您可以: foo ='abc'; bar ='def'; baz = $ {foo} $ {bar} –

0

正如@jfsebastian指出的那樣,virtualenvwrapper完成了你所要求的大部分或全部功能。

http://virtualenvwrapper.readthedocs.org/

virtualenvwrapper是一組擴展伊恩Bicking的virtualenv中 工具。這些擴展包括用於創建和刪除虛擬環境的包裝器,並以其他方式管理您的開發工作流程, 可以更輕鬆地在一個時間處理多個項目,而不會在其相關性中引入衝突 。

相關問題