Difference between revisions of "Python/Virtualenv"

From ArchWiki
Jump to: navigation, search
(Fix python version check)
m (Add zh-cn page link.)
 
(19 intermediate revisions by 14 users not shown)
Line 1: Line 1:
 
[[Category:Development]]
 
[[Category:Development]]
{{Out of date}}
+
[[es:Python VirtualEnv]]
 +
[[ja:Python VirtualEnv]]
 +
[[zh-cn:Python/Virtualenv]]
 +
{{Expansion|1=Does not describe the difference between {{ic|virtualenv}} and {{ic|python -m venv}}. See also [https://wiki.archlinux.org/index.php?title=ArchWiki:Reports&diff=343331&oldid=343147 this report].}}
 +
{{Poor writing|stretched writing}}
  
''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 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.
 
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.
  
Line 9: Line 13:
  
 
''vitualenvwrapper'' takes ''virtualenv'' a step further by providing convenient commands you can invoke from your favorite console.
 
''vitualenvwrapper'' takes ''virtualenv'' a step further by providing convenient commands you can invoke from your favorite console.
 +
 +
''venv'' is [https://docs.python.org/3/library/venv.html a built-in module] added in version 3.3, implementing a similar API to ''virtualenv''.
  
 
== Virtualenv ==
 
== Virtualenv ==
  
''virtualenv'' supports Python 2.5+ and Python 3.x.
+
''virtualenv'' supports Python 2.6+ and Python 3.x. See [[Python#Python 3]] for an overview of the different versions of Python.
  
===Installation===
+
=== Installation ===
Simply install from the community repository:
+
 
# pacman -S python2-virtualenv
+
[[Install]] {{Pkg|python-virtualenv}}, or {{Pkg|python2-virtualenv}} for the legacy version.
or
+
# pacman -S python-virtualenv
+
  
 
===Basic Usage===
 
===Basic Usage===
An extended tutorial on how use ''virtualenv'' for sandboxing can be found [http://wiki.pylonshq.com/display/pylonscookbook/Using+a+Virtualenv+Sandbox here].
+
An extended tutorial on how use ''virtualenv'' for sandboxing can be found [http://wiki.pylonshq.com/display/pylonscookbook/Using+a+Virtualenv+Sandbox here]. If the link does not work, try [http://web.archive.org/web/20120304235158/http://wiki.pylonshq.com/display/pylonscookbook/Using+a+Virtualenv+Sandbox the archive link]. A simple use case is as follows:
  
The typical use case is:
+
* Create a virtualenv:
* Create a folder for the new virtualenv:
+
  $ virtualenv my_env
  $ mkdir -p ~/.virtualenvs/my_env
+
* Activate the virtualenv:
* Create the virtualenv:
+
  $ source my_env/bin/activate
$ virtualenv2 ~/.virtualenvs/my_env
+
* Activate the virtualenv:  
+
  $ source ~/.virtualenvs/my_env/bin/activate
+
 
* Install some package inside the virtualenv (say, Django):
 
* Install some package inside the virtualenv (say, Django):
 
  (my_env)$ pip install django
 
  (my_env)$ pip install django
Line 40: Line 41:
 
''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 {{Pkg|python-virtualenv}} and {{Pkg|python2-virtualenv}}.
 
''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 {{Pkg|python-virtualenv}} and {{Pkg|python2-virtualenv}}.
 
===Installation===
 
===Installation===
[[pacman|Install]] the {{Pkg|python-virtualenvwrapper}} package from the [[Official Repositories|official repositories]].
+
[[Install]] the {{Pkg|python-virtualenvwrapper}} package from the [[official repositories]].
  
 
Now add the following lines to your {{ic|~/.bashrc}}:
 
Now add the following lines to your {{ic|~/.bashrc}}:
Line 58: Line 59:
  
 
* Create the virtualenv:
 
* Create the virtualenv:
  $ mkvirtualenv -p python2.7 my_env
+
  $ mkvirtualenv -p /usr/bin/python2.7 my_env
 
* Activate the virtualenv:
 
* Activate the virtualenv:
 
  $ workon my_env
 
  $ workon my_env
 
* Install some package inside the virtualenv (say, Django):
 
* Install some package inside the virtualenv (say, Django):
  $ (my_env)$ pip install django
+
  (my_env)$ pip install django
 
* Do your things
 
* Do your things
 
* Leave the virtualenv:  
 
* Leave the virtualenv:  
Line 68: Line 69:
  
 
== See Also ==
 
== See Also ==
*[http://pypi.python.org/pypi/virtualenv virtualenv Pypi page]
+
*[https://pypi.python.org/pypi/virtualenv virtualenv Pypi page]
 
*[http://wiki.pylonshq.com/display/pylonscookbook/Using+a+Virtualenv+Sandbox Tutorial for virtualenv]
 
*[http://wiki.pylonshq.com/display/pylonscookbook/Using+a+Virtualenv+Sandbox Tutorial for virtualenv]
 
*[http://www.doughellmann.com/docs/virtualenvwrapper/ virtualenvwrapper page at Doug Hellmann's]
 
*[http://www.doughellmann.com/docs/virtualenvwrapper/ virtualenvwrapper page at Doug Hellmann's]

Latest revision as of 01:53, 31 March 2016

Tango-view-fullscreen.pngThis article or section needs expansion.Tango-view-fullscreen.png

Reason: Does not describe the difference between virtualenv and python -m venv. See also this report. (Discuss in Talk:Python/Virtualenv#)

Tango-edit-clear.pngThis article or section needs language, wiki syntax or style improvements.Tango-edit-clear.png

Reason: stretched writing (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.

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:

  • Create a virtualenv:
$ virtualenv my_env
  • Activate the virtualenv:
$ source 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. 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

See Also