Difference between revisions of "Python/Virtual environment"

From ArchWiki
Jump to: navigation, search
(Created page with "Category:Development (English) ==Introduction== ''virtualenv'' is a python tool written by Ian Bicking and used to create isolated environments for python in which you can i...")
 
m (Random fixes (mostly typos).)
Line 2: Line 2:
  
 
==Introduction==
 
==Introduction==
''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.
+
''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 cover 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.
+
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==
 
==Virtual Environments at a glance==
''virtualenv'' is a tool designated to address the problem of dealing with packages' dependencies while maintaining different versions that suite 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 to isolated environment, to have the two development environment to play along nicely.
+
''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'' take ''virtualenv'' a step further by providing convenient command you can invoke from your favourite console.
+
''vitualenvwrapper'' takes ''virtualenv'' a step further by providing convenient commands you can invoke from your favorite console.
  
  
 
== Virtualenv ==
 
== Virtualenv ==
  
Currently ''virtualenv'' only support Python up to version 2.7. If you really need virtual environment on Python 3 check out the virtualenv3 project on Bitbucket : http://bitbucket.org/brandon/virtualenv3.
+
Currently ''virtualenv'' only supports Python up to version 2.7. If you really need virtual environment on Python 3, check out the [http://bitbucket.org/brandon/virtualenv3 virtualenv3] project on Bitbucket.
  
 
===Installation===
 
===Installation===
Simply install python-virtualenv from the community repository and you're done :
+
Simply install python-virtualenv from the community repository and you're done:
 
  # pacman -S python-virtualenv
 
  # pacman -S python-virtualenv
  
Line 23: Line 23:
  
 
The typical use case is:
 
The typical use case is:
* Create a folder for the new virtualenv :  
+
* Create a folder for the new virtualenv:
 
  $ mkdir ~/.virtualenvs/my_env
 
  $ mkdir ~/.virtualenvs/my_env
* Create the virtualenv, here without package inheritance from the system's installation :  
+
* Create the virtualenv, here without package inheritance from the system's installation:  
 
  $ virtualenv --no-site-pacakges ~/.virtualenvs/my_env
 
  $ virtualenv --no-site-pacakges ~/.virtualenvs/my_env
* Activate he virtualenv :  
+
* Activate the virtualenv:  
 
  $ source ~/.virtualenvs/my_env/bin/activate
 
  $ source ~/.virtualenvs/my_env/bin/activate
* Install some package inside the virtualenv (say, Django) :  
+
* Install some package inside the virtualenv (say, Django):
 
  (my_env)$ easy_install django
 
  (my_env)$ easy_install django
 
* Do your things
 
* Do your things
* Leave the virtualenv :  
+
* Leave the virtualenv:
 
  (my_env)$ deactivate
 
  (my_env)$ deactivate
  
 
== Virtualenvwrapper ==
 
== Virtualenvwrapper ==
  
''virtualenvwrapper'' allow more natural command line interactions with your virtualenvs by exposing several usefull commands to create, activate and remove virtualenvs. As ''virtualenv'' this package does not support currently Python 3.x.
+
''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===
 
===Installation===
Install python-virtualenvwrapper from community, if you have not installed python-virtualenv yet, it will be installed now as a dependency.
+
Install python-virtualenvwrapper from community. If you have not installed python-virtualenv yet, it will be installed now as a dependency.
 
  # pacman -S python-virtualenvwrapper
 
  # pacman -S python-virtualenvwrapper
  
Now add the following line to your .bashrc :
+
Now add the following lines to your .bashrc:
source /usr/bin/virtualenvwrapper.sh
+
 
  export WORKON_HOME=~/.virtualenvs
 
  export WORKON_HOME=~/.virtualenvs
 +
source /usr/bin/virtualenvwrapper.sh
  
Re-open your console and create the WORKON_HOME folder :
+
Re-open your console and create the WORKON_HOME folder:
 
  $ mkdir $WORKON_HOME
 
  $ mkdir $WORKON_HOME
  
 
===Basic Usage===
 
===Basic Usage===
The main information source on virtualenvwrapper usage (and extension capability) is Doug Hellmann's page : http://www.doughellmann.com/docs/virtualenvwrapper/.
+
The main information source on virtualenvwrapper usage (and extension capability) is Doug Hellmann's [http://www.doughellmann.com/docs/virtualenvwrapper/ page].
  
* Create the virtualenv:  
+
* Create the virtualenv:
 
  $ mkvirtualenv --no-site-pacakges my_env
 
  $ mkvirtualenv --no-site-pacakges my_env
* Activate he virtualenv :  
+
* Activate the virtualenv:
 
  $ workon my_enve
 
  $ workon my_enve
* Install some package inside the virtualenv (say, Django) : (my_env)$ easy_install django
+
* Install some package inside the virtualenv (say, Django):
 +
$ (my_env)$ easy_install django
 
* Do your things
 
* Do your things
* Leave the virtualenv :  
+
* Leave the virtualenv:  
 
  (my_env)$ deactivate
 
  (my_env)$ deactivate
  

Revision as of 21:58, 15 November 2010


Introduction

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 python-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:
$ virtualenv --no-site-pacakges ~/.virtualenvs/my_env
  • Activate the virtualenv:
$ source ~/.virtualenvs/my_env/bin/activate
  • Install some package inside the virtualenv (say, Django):
(my_env)$ easy_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 python-virtualenvwrapper from community. If you have not installed python-virtualenv yet, it will be installed now as a dependency.

# pacman -S python-virtualenvwrapper

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 --no-site-pacakges my_env
  • Activate the virtualenv:
$ workon my_enve
  • Install some package inside the virtualenv (say, Django):
$ (my_env)$ easy_install django
  • Do your things
  • Leave the virtualenv:
(my_env)$ deactivate

See Also