8

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

我可以只安装 python-setuptools、python-virtualenv 和 python-pip 系统包,然后继续我的快乐之路,但我也希望能够获得最新/特定版本的 Distribute、virtualenv 和 pip。这些没有 PPA,所以我必须手动安装它们。

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

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

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

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

4

3 回答 3

7

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

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

所以工作流程是:

  • 在您的系统上安装 Python 版本 X
  • 下载virtualenv源码版本Q(可能是最新的)
  • 使用 Python X 和 virtualenv Q 创建新的虚拟环境
  • 您的新 VE 现在正在运行 Python X 和最新的稳定版本的 pip 和分发
  • 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

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

用户@计算机:/home/user$ 。env_create python3 /media/work/environments/test
/media/work/environments/test/bin/python3 中的新 python 可执行文件
还在 /media/work/environments/test/bin/python 中创建可执行文件
安装分发......完成。
安装 pip……完成。
蟒蛇 3.2
分发==0.6.19
wsgiref==0.1.2
用户@计算机:/媒体/工作/环境/测试$
于 2011-07-29T15:04:19.787 回答
0

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

virtualenvwrapper 是 Ian Bicking 的 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 回答
0

正如@jfsebastian 所指出的,virtualenvwrapper 可以完成您所要求的大部分或全部工作。

http://virtualenvwrapper.readthedocs.org/

virtualenvwrapper 是 Ian Bicking 的 virtualenv 工具的一组扩展。这些扩展包括用于创建和删除虚拟环境以及以其他方式管理您的开发工作流程的包装器,从而更容易一次处理多个项目,而不会在它们的依赖项中引入冲突。

于 2013-10-23T20:46:24.700 回答