如何在Ubuntu上正确安装多个非包的Distribute/virtualenv/pip生态系统?
我正在Ubuntu上开发Python应用程序。我想建立一个Distribute/virtualenv/pip生态系统,这样我就可以独立管理我的Python包,而不影响系统自带的Python包(那些我在Synaptic里管理,或者说是让系统帮我管理的)。
我可以直接安装python-setuptools、python-virtualenv和python-pip这些系统包,然后就可以开始使用了,但我还想获取Distribute、virtualenv和pip的最新版本或特定版本。因为没有相关的PPA(个人包存档),所以我得手动安装它们。
还有一个麻烦的是,我想为多个版本的Python做这个设置。也就是说,我想为python2.6设置一个生态系统,为python设置一个,为python3设置一个,或者在64位系统上为chroot的32位Python设置一个。
我猜这个过程大概是这样的:
- 用Python X在我的主文件夹里安装自己的Distribute副本
- 用独立的Distribute,使用easy_install安装pip
- 用独立的pip,安装virtualenv
- 用独立的virtualenv,创建虚拟环境
- 激活虚拟环境,安装包
- 对Python Y、Z和Q重复以上步骤
我需要关注哪些安装和配置选项呢?
3 个回答
正如@j.f.sebastian提到的,virtualenvwrapper可以满足你大部分甚至全部的需求。
http://virtualenvwrapper.readthedocs.org/
virtualenvwrapper是对Ian Bicking的virtualenv工具的一些扩展。它提供了一些功能,可以帮助你创建和删除虚拟环境,还能管理你的开发流程。这样,你就可以同时进行多个项目的开发,而不会因为项目之间的依赖关系产生冲突。
根据Walker Hale IV的回答,要做到这一点有两个关键点:
- 你不需要单独安装distribute和pip,因为它们会自动包含在新的虚拟环境中(而且你大概只想要那些已经和virtualenv测试过的版本)
- 你可以使用virtualenv的源代码来创建新的虚拟环境,而不是使用系统中安装的virtualenv版本
所以整个流程是:
- 在你的系统上安装Python版本X
- 下载virtualenv源代码版本Q(可能是最新的)
- 用Python X和virtualenv Q创建新的虚拟环境
- 你的新虚拟环境现在运行的是Python X和最新稳定版本的pip和distribute
- 使用pip安装其他任何包
- 使用easy_install安装那些你不能用pip安装的包
注意事项:
- 在你的新虚拟环境中,你可以安装新的(或旧的)版本的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/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 user@computer:/media/work/environments/test$
进一步解释一下JF Sebastian和nealmcb的贡献,最近我确实在使用我系统自带的virtualenvwrapper(在Ubuntu 12.04及之后的版本中可用)。
virtualenvwrapper是对Ian Bicking的virtualenv工具的一组扩展。它的扩展功能包括创建和删除虚拟环境的工具,以及其他管理开发工作流程的功能,让你可以更轻松地同时处理多个项目,而不会在它们的依赖关系上产生冲突。
我使用的主要功能(回答这个问题时提到的)有:
mkvirtualenv --python=PYTHON_EXE
可以用特定的Python执行文件创建一个虚拟环境(不一定要是系统自带的版本)- 与pip的虚拟环境支持结合
allvirtualenv pip install -U pip
可以在所有虚拟环境中升级pip
JFS提到的环境变量确实很有用,可以进行调整:PIP_DOWNLOAD_CACHE、VIRTUALENV_USE_DISTRIBUTE、WORKON_HOME、VIRTUALENVWRAPPER_PYTHON。
更新virtualenv的唯一原因是为了获得最新版本的setuptools(之前叫Distribute,更早之前叫setuptools)。我还没有需要这样做的情况,但我猜从一个新的虚拟环境开始,先升级Distribute/setuptools,然后再升级pip,最后安装其他库,会是最简单的方式。
如果确实需要一个新版本的virtualenv,可以修改引导脚本来实现。