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

发布于 2024-11-26 12:47:51 字数 948 浏览 4 评论 0原文

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

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

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

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

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

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

I am developing Python applications in Ubuntu. I want to setup a Distribute/virtualenv/pip ecosystem to manage my Python packages independently of any system Python packages (which I manage in Synaptic, or rather I let the system manage them for me).

I could just install the python-setuptools, python-virtualenv and python-pip system packages and be on my merry way, but I also want to be able to get latest/specific versions of Distribute, virtualenv and pip. There are no PPAs for these, so I'll have to install them manually.

A final complication, is that I want to be able to do this for multiple versions of Python. That is, set up one ecosystem for python2.6, another for python, another for python3, or on a 64-bit system another for chrooted 32-bit Python.

I'm guessing that the process would be something like:

  • Using Python X install my own copy of Distribute to a location in my home folder
  • Using indie Distribute, easy_install pip
  • Using indie pip, install virtualenv
  • Using indie virtualenv, create virtual environment
  • Activate virtual environment, install packages
  • Repeat for Python Y, Z and Q

What installation/configuration options am I looking for?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

凡间太子 2024-12-03 12:47:51

基于 沃克·黑尔IV 对类似(但不同!;))问题的回答,执行此操作有两个关键:

  • 您不需要安装分发和pip 因为这些会自动包含在新的虚拟环境中(并且您可能只需要已经使用 virtualenv 测试过的版本),
  • 您可以使用 virtualenv 源代码创建新的虚拟环境,而不是使用系统安装的 virtualenv 版本

因此,工作流程是:

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

注意:

  • 在新的虚拟环境中,您可以安装新(或旧)版本的 distribution、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

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

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$ 

Based on Walker Hale IV's answer to a similar (but distinct! ;) ) question, there are two keys to doing this:

  • you don't need to install distribute and pip because these are automatically included in a new virtual environment (and you presumably only want the versions that have been tested with virtualenv)
  • you can use the virtualenv source code to create a new virtual environment, rather than using the system-installed version of virtualenv

So the workflow is:

  • install Python version X on your system
  • download virtualenv source code version Q (probably the latest)
  • create new virtual environment with Python X and virtualenv Q
  • your new VE is now running Python X and the latest stable versions of pip and distribute
  • pip install any other packages
  • easy_install any other packages that you can't pip install

Notes:

  • In your new virtual environment, you could install new (or old) versions of distribute, pip or virtualenv. (I think)
  • I don't use WH4's technique of create a bootstrap virtual environment. Instead, I create the new virtual environment from the virtualenv source each time.
  • This technique should be usable on any operating system.
  • If I were explaining this to someone new to the whole Distribute/pip/virtualenv ecosystem concept, I would explain it in a virtualenv-centric way.

I've written a bash script that does the basics in 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

Output looks something like this (with downloading switched off and deactivation switched on):

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$ 
七堇年 2024-12-03 12:47:51

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

http://virtualenvwrapper.readthedocs.org/

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

As noted by @j.f.sebastian, virtualenvwrapper does much or all of what you're asking for.

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.

梦醒灬来后我 2024-12-03 12:47:51

详细阐述 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,则应该修改引导脚本

Elaborating on JF Sebastian and nealmcb's contributions, these days I do indeed use my system packaged version of virtualenvwrapper (available on Ubuntu 12.04 and later).

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.

The key features I use (in answer to this question) are:

The environment variables JFS mentioned are indeed useful to fiddle with: PIP_DOWNLOAD_CACHE, VIRTUALENV_USE_DISTRIBUTE, WORKON_HOME, VIRTUALENVWRAPPER_PYTHON.

The only reason to update virtualenv itself is to get the latest version of setuptools (previously known as Distribute, previously known as setuptools). I haven't had a need to do this yet, but I suspect it would be easiest to start with a fresh virtualenv and upgrade Distribute/setuptools first, then upgrade pip, then install other libraries.

If a new version of virtualenv is strictly necessary, a modification of the bootstrap script should do.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文