Python/Virtualenv

From ArchWiki
Revision as of 12:10, 28 December 2011 by Kynikos (Talk | contribs) (Undo revision 176001 by Varunsharma (talk) please read talk pages before removing article status templates)

Jump to: navigation, search

This template has only maintenance purposes. For linking to local translations please use interlanguage links, see Help:i18n#Interlanguage links.


Local languages: Català – Dansk – English – Español – Esperanto – Hrvatski – Indonesia – Italiano – Lietuviškai – Magyar – Nederlands – Norsk Bokmål – Polski – Português – Slovenský – Česky – Ελληνικά – Български – Русский – Српски – Українська – עברית – العربية – ไทย – 日本語 – 正體中文 – 简体中文 – 한국어


External languages (all articles in these languages should be moved to the external wiki): Deutsch – Français – Română – Suomi – Svenska – Tiếng Việt – Türkçe – فارسی

Tango-view-refresh-red.pngThis article or section is out of date.Tango-view-refresh-red.png

Reason: please use the first argument of the template to provide a brief explanation. (Discuss in Talk:Python/Virtualenv#)

virtualenv is a Python tool written by Ian Bicking and used to create isolated environments for Python in which you can install packages without interfering with the other virtualenvs nor with the system Python's packages. 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.

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.

Virtualenv

Currently virtualenv only supports Python up to version 2.7. If you really need virtual environment on Python 3, check out the virtualenv3 project on Bitbucket.

Installation

Simply install python-virtualenv from the community repository and you're done:

# pacman -S python2-virtualenv

Basic Usage

An extended tutorial on how use virtualenv for sandboxing can be found here.

The typical use case is:

  • Create a folder for the new virtualenv:
$ mkdir ~/.virtualenvs/my_env
  • Create the virtualenv, here without package inheritance from the system's installation:
$ virtualenv2 --no-site-packages ~/.virtualenvs/my_env
  • Activate the virtualenv:
$ source ~/.virtualenvs/my_env/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. Like virtualenv, this package does not currently support Python 3.x.

Installation

Install the python-virtualenvwrapper package from the official repositories. If you have not installed python-virtualenv yet, python-virtualenvwrapper will be installed now as a dependency.

Now add the following lines to your ~/.bashrc:

export WORKON_HOME=~/.virtualenvs
source /usr/bin/virtualenvwrapper.sh

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 python2.7 --no-site-packages 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

See Also