我正在Ubuntu中开发Python应用程序。我想设置一个Distribute/virtualenv/pip ecosystem来独立于任何系统Python包管理我的Python包(我在Synaptic中管理,或者我让系统为我管理它们。)
我可以安装python-setuptools,python-virtualenv和python-pip系统软件包,并以我的快乐方式,但我也希望能够获得最新/特定版本的Distribute,virtualenv和pip。这些没有PPA,所以我必须手动安装它们。
最后的复杂情况是,我希望能够为多个版本的Python执行此操作。也就是说,为python2.6设置一个生态系统,为python设置另一个生态系统,为python3设置另一个生态系统,或为chrooted 32-bit Python设置另一个生态系统。
我猜这个过程会是这样的:
我在寻找哪些安装/配置选项?
答案 0 :(得分:7)
根据Walker Hale IV's answer基于类似(但不同的!;))的问题,有两个关键要做:
所以工作流程是:
注意:
我编写了一个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
输出看起来像这样(关闭下载并关闭去激活):
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$
答案 1 :(得分:0)
正如@ j.f.sebastian所指出的,virtualenvwrapper可以满足你所要求的大部分或全部。
http://virtualenvwrapper.readthedocs.org/
virtualenvwrapper是Ian Bicking的virtualenv的一组扩展 工具。扩展包括用于创建和删除的包装器 虚拟环境以及管理开发工作流程, 在没有的情况下,一次可以更容易地处理多个项目 在他们的依赖中引入冲突。
答案 2 :(得分:0)
阐述JF Sebastian和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。