Python/Virtualenv (简体中文)
virtualenv 是 Ian Bicking 编写的 Python 工具,可以为 Python 建立独立环境,可以安装软件包而不影响其它 virtualenv 环境或系统 Python 软件包。 The present article covers the installation of the virtualenv package and its companion command line utility virtualenvwrapper designed by Doug Hellmann to (greatly) improve your work flow. A quick how-to to help you to begin working inside virtual environment is then provided.
Contents
Virtual Environments at a glance
virtualenv is a tool designated to address the problem of dealing with packages' dependencies while maintaining different versions that suit projects' needs. For example, if you work on two Django web sites, say one that needs Django 1.2 while the other needs the good old 0.96. You have no way to keep both versions if you install them into /usr/lib/python2/site-packages . Thanks to virtualenv it's possible, by creating two isolated environments, to have the two development environment to play along nicely.
vitualenvwrapper takes virtualenv a step further by providing convenient commands you can invoke from your favorite console.
venv is a built-in module added in version 3.3, implementing a similar API to virtualenv.
Virtualenv
virtualenv supports Python 2.6+ and Python 3.x. See Python#Python 3 for an overview of the different versions of Python.
Installation
Install python-virtualenv, or python2-virtualenv for the legacy version.
Basic Usage
An extended tutorial on how use virtualenv for sandboxing can be found here. If the link does not work, try the archive link. A simple use case is as follows:
- 创建 Virtualenv 环境:
$ virtualenv2 venv # 创建一个名为 venv 的目录,其中包含了 Virtualenv
- 激活这个Virtualenv:
$ source venv/bin/activate
- Install some package inside the virtualenv (say, Django):
(my_env)$ pip install django
- Do your things
- Leave the virtualenv:
(my_env)$ deactivate
Virtualenvwrapper
virtualenvwrapper allows more natural command line interaction with your virtualenvs by exposing several useful commands to create, activate and remove virtualenvs. This package is a wrapper for both python-virtualenv and python2-virtualenv.
Installation
Install the python-virtualenvwrapper package from the official repositories.
Now add the following lines to your ~/.bashrc
:
export WORKON_HOME=~/.virtualenvs source /usr/bin/virtualenvwrapper.sh
If you are not using python3 by default (check the output of $ python --version
) you also need to add the following line to your ~/.bashrc
prior sourcing the virtualenvwrapper.sh
script. The current version of the virtualenvwrapper-python
package only works with python3. It can create python2 virtualenvs fine though.
VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3
Re-open your console and create the WORKON_HOME
folder:
$ mkdir $WORKON_HOME
Basic Usage
The main information source on virtualenvwrapper usage (and extension capability) is Doug Hellmann's page.
- Create the virtualenv:
$ mkvirtualenv -p /usr/bin/python2.7 my_env
- Activate the virtualenv:
$ workon my_env
- Install some package inside the virtualenv (say, Django):
(my_env)$ pip install django
- Do your things
- Leave the virtualenv:
(my_env)$ deactivate