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

64 阅读3分钟

在 Ubuntu 系统中,对于需要独立管理 Python 包的 Python 应用程序,用户可能希望设置一个 Distribute/virtualenv/pip 生态系统。然而,手动安装这些工具通常会有安装费用的问题。此外,用户通常也希望为多个版本的 Python 设置生态系统,例如 Python 2.6、Python 3 或 Python 3.2。如何选择合适的安装和配置选项来解决这些问题?

2、解决方案

解决方案概述

根据一个相关问题的回答,解决方法的关键在于:

  1. 无需单独安装 Distribute 和 pip,因为它们已经在新的虚拟环境中自动包含。
  2. 可以使用 virtualenv 源代码来创建一个新的虚拟环境,而不是使用系统上安装的 virtualenv 版本。

详细步骤

因此,具体的操作步骤如下:

  1. 在系统中安装 Python 版本 X。
  2. 下载 virtualenv 源代码版本 Q(建议为最新版本)。
  3. 使用 Python X 和 virtualenv Q 创建一个新的虚拟环境。
  4. 新的虚拟环境现在正在运行 Python X 和最新稳定版本的 pip 和 Distribute。
  5. 使用 pip 安装其他软件包。
  6. 使用 easy_install 安装其他无法通过 pip 安装的软件包。

注意

  1. 在新的虚拟环境中,可以安装 Distribute、pip 或 virtualenv 的新版本或旧版本。
  2. 不使用 WH4 的引导虚拟环境技术,而是每次都从 virtualenv 源代码创建新的虚拟环境。
  3. 该解决方案应该可以在任何操作系统上使用。

Bash 脚本示例

有用户提供了一个 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

希望这份技术文章能够帮助您在 Ubuntu 上正确安装多个非包 Distribute/virtualenv/pip 生态系统。