如何在Ubuntu上正确安装多个非软件包Distribute/virtualenv/pip生态系统?

dav*_*one 8 python ubuntu pip virtualenv distribute

我正在Ubuntu中开发Python应用程序.我想设置一个Distribute/virtualenv/pip生态系统来独立于任何系统Python包管理我的Python包(我在Synaptic中管理,或者我让系统为我管理它们).

我可以安装python-setuptools,python-virtualenv和python-pip系统软件包,并以我的快乐方式,但我也希望能够获得最新/特定版本的Distribute,virtualenv和pip.这些没有PPA,所以我必须手动安装它们.

最后的复杂情况是,我希望能够为多个版本的Python执行此操作.也就是说,为python2.6设置一个生态系统,为python设置另一个生态系统,为python3设置另一个生态系统,或者为chrooted 32位Python设置另一个生态系统.

我猜这个过程会是这样的:

  • 使用Python X将我自己的Distribute副本安装到我的主文件夹中的某个位置
  • 使用indie Distribute,easy_install pip
  • 使用indie pip,安装virtualenv
  • 使用indie virtualenv,创建虚拟环境
  • 激活虚拟环境,安装包
  • 重复Python Y,Z和Q.

我在寻找哪些安装/配置选项?

dav*_*one 7

根据Walker Hale IV对类似(但不同的!;))问题的回答,有两个关键要做:

  • 你不需要安装distribute和pip,因为它们会自动包含在一个新的虚拟环境中(你可能只想要使用virtualenv测试过的版本)
  • 你可以使用virtualenv源代码来创建一个新的虚拟环境,而不是使用系统安装的virtualenv版本

所以工作流程是:

  • 在您的系统上安装Python版本X.
  • 下载virtualenv源代码版Q(可能是最新的)
  • 使用Python X和virtualenv Q创建新的虚拟环境
  • 您的新VE现在正在运行Python X以及最新稳定版本的pip和分发
  • pip安装任何其他包
  • easy_install你无法安装的任何其他软件包

笔记:

  • 在新的虚拟环境中,您可以安装distribute(pip或virtualenv)的新(或旧)版本.(我认为)
  • 我不使用WH4创建引导虚拟环境的技术.相反,我每次都会从virtualenv源创建新的虚拟环境.
  • 此技术应该可以在任何操作系统上使用.
  • 如果我向整个Distribute/pip/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
Run Code Online (Sandbox Code Playgroud)

输出看起来像这样(下载关闭和停用开启):

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$ 

  • [virtualenvwrapper](http://virtualenvwrapper.readthedocs.org/en/latest/)简化了管理多个虚拟环境的过程,例如`workon`,`mkvirtualenv` /`rmvirtualenv`命令.我还发现`PIP_DOWNLOAD_CACHE`,`VIRTUALENV_USE_DISTRIBUTE`,`WORKON_HOME`,`VIRTUALENVWRAPPER_PYTHON`环境变量很有用. (2认同)

dav*_*one 0

详细阐述 JF Sebastian 和 nealmcb\ 的贡献,这些天我确实使用了virtualenvwrapper的系统打包版本(在 Ubuntu 12.04 及更高版本上可用)。

\n\n
\n

virtualenvwrapper 是 Ian Bicking\xe2\x80\x99s virtualenv 工具的一组扩展。这些扩展包括用于创建和删除虚拟环境以及以其他方式管理开发工作流程的包装器,使您可以更轻松地同时处理多个项目,而不会在其依赖项中引入冲突。

\n
\n\n

我使用(回答这个问题)的主要功能是:

\n\n\n\n

JFS提到的环境变量确实对摆弄很有用:PIP_DOWNLOAD_CACHE、VIRTUALENV_USE_DISTRIBUTE、WORKON_HOME、VIRTUALENVWRAPPER_PYTHON。

\n\n

更新 virtualenv 本身的唯一原因是获取最新版本的 setuptools(以前称为 Distribute,以前称为 setuptools)。我还没有必要这样做,但我怀疑从一个新的 virtualenv 开始并首先升级 Distribute/setuptools,然后升级 pip,然后安装其他库是最简单的。

\n\n

如果绝对需要新版本的 virtualenv,则应该修改引导脚本。

\n