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

2024-06-25 23:18:46 发布

您现在位置:Python中文网/ 问答频道 /正文

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

我只需要安装python setuptools、python virtualenv和python pip系统包就可以了,但我也希望能够获得Distribute、virtualenv和pip的最新/特定版本。这些没有PPA,所以我必须手动安装。在

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

我猜这个过程大概是:

  • 使用pythonx将我自己的分发副本安装到主文件夹中的某个位置
  • 使用独立分布,轻松安装pip
  • 使用indie pip安装virtualenv
  • 使用indie virtualenv创建虚拟环境
  • 激活虚拟环境,安装软件包
  • 对Python Y、Z和Q重复上述操作

我需要什么安装/配置选项?在


Tags: pip版本应用程序virtualenvubuntu系统虚拟环境手动
3条回答

基于Walker Hale IV's answer到一个相似的(但是不同的!;))问题,要做到这一点有两个关键:

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

所以工作流程是:

  • 在系统上安装pythonversionx
  • 下载virtualenv源代码版本Q(可能是最新版本)
  • 使用pythonx和virtualenvq创建新的虚拟环境
  • 您的新VE现在正在运行pythonx和pip和distribute的最新稳定版本
  • pip安装任何其他软件包
  • easy_安装任何其他无法pip安装的软件包

注意事项:

  • 在新的虚拟环境中,您可以安装distribute、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

输出如下所示(下载关闭,停用打开):

^{pr2}$

正如@j.f.sebastian指出的,virtualenvwrapper做了很多或所有你想要的。在

http://virtualenvwrapper.readthedocs.org/

virtualenvwrapper is a set of extensions to Ian Bicking’s virtualenv tool. The extensions include wrappers for creating and deleting virtual environments and otherwise managing your development workflow, making it easier to work on more than one project at a time without introducing conflicts in their dependencies.

在详细介绍jfsebastian和nealmcb的贡献之后,这些天我确实使用了我的系统打包版本virtualenvwrapper(可在ubuntu12.04及更高版本上获得)。在

virtualenvwrapper is a set of extensions to Ian Bicking’s virtualenv tool. The extensions include wrappers for creating and deleting virtual environments and otherwise managing your development workflow, making it easier to work on more than one project at a time without introducing conflicts in their dependencies.

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

  • mkvirtualenv python=PYTHON_EXE使用特定的Python可执行文件创建virtualenv(不必是系统打包的版本)
  • tie in to pip's virtualenv support
  • ^{}升级所有虚拟机中的pip

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

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

如果virtualenv的新版本是绝对必要的,那么the bootstrap script的修改就可以了。在

相关问题 更多 >