Difference between revisions of "Python/Virtualenv"

From ArchWiki
Jump to: navigation, search
m (Remove `cd` from activation, add creation instruction and note about version control)
 
(27 intermediate revisions by 19 users not shown)
Line 1: Line 1:
 
[[Category:Development]]
 
[[Category:Development]]
{{Out of date}}
+
[[es:Python VirtualEnv]]
 +
[[ja:Python VirtualEnv]]
 +
[[zh-cn: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.
+
''virtualenv'' is a tool used to create an isolated workspace for a [[Python]] application. It has various advantages such as the ability to install modules locally, export a working environment, and execute a [[Python]] program in that environment.
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==
+
==Overview==
''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.
+
A virtual environment is a directory into which some binaries and shell scripts are installed. The binaries include {{ic|python}} for executing scripts and {{ic|pip}} for installing other modules within the environment. There are also shell scripts (one for [[bash]], csh, and [[fish]]) to activate the environment. Essentially, a virtual environment mimics a full system install of [[Python]] and all of the desired modules without interfering with any system on which the application might run.
  
''vitualenvwrapper'' takes ''virtualenv'' a step further by providing convenient commands you can invoke from your favorite console.
+
==Installation==
 +
[[Python]] 3.3+ comes with a tool called {{ic|pyvenv}} and an API called ''venv'' for extending the native implementation. For applications that require an older version of Python, {{ic|virtualenv}} must be used.
  
== Virtualenv ==
+
===Packages===
 +
[[Install]] one of these packages from the [[official repositories]] to use a Python virtual environment.
 +
* Python 3.3+: {{pkg|python}}
 +
* Python 3: {{pkg|python-virtualenv}}
 +
* Python 2: {{pkg|python2-virtualenv}}
  
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.
+
==Usage==
 +
All three tools use a similar workflow.
  
===Installation===
+
===Creation===
Simply install python-virtualenv from the community repository and you're done:
+
Use {{ic|pyvenv}} or {{ic|virtualenv}} to create the virtual environment within your project directory. Be sure to exclude the venv directory from version control--a copy of {{ic|pip freeze}} will be enough to rebuild it.
# pacman -S python2-virtualenv
+
  
===Basic Usage===
+
====pyvenv====
An extended tutorial on how use ''virtualenv'' for sandboxing can be found [http://wiki.pylonshq.com/display/pylonscookbook/Using+a+Virtualenv+Sandbox here].
+
This tool is provided by {{pkg|python}} (3.3+).
 +
$ pyvenv venv
  
The typical use case is:
+
====virtualenv====
* Create a folder for the new virtualenv:
+
Use {{ic|virtualenv}} for Python 3, available in {{pkg|python-virtualenv}}.
  $ mkdir -p ~/.virtualenvs/my_env
+
  $ virtualenv venv
* Create the virtualenv:
+
 
  $ virtualenv2 ~/.virtualenvs/my_env
+
And {{ic|virtualenv2}} for Python 2, available in {{pkg|python2-virtualenv}}.
* Activate the virtualenv:
+
  $ virtualenv2 venv
  $ source ~/.virtualenvs/my_env/bin/activate
+
 
* Install some package inside the virtualenv (say, Django):
+
===Activation===
  (my_env)$ pip install django
+
Use one of the provided shell scripts to activate and deactivate the environment. This example assumes bash is used.
* Do your things
+
  $ source venv/bin/activate
* Leave the virtualenv:
+
(venv) $ which python
  (my_env)$ deactivate
+
(venv) $ deactivate  # this is a shell function provided by bin/activate
 +
$ which python
 +
 
 +
===Normal Activities===
 +
Once inside the virtual environment, modules can be installed and scripts can be run as normal:
 +
  (venv) $ python -c 'import requests; print("'''this will not display because requests is not installed'''")'
 +
(venv) $ pip install requests
 +
(venv) $ python -c 'import requests; print("'''requests is now installed in the virtual environment'''")'
 +
 
 +
==Python Versions==
 +
The binary versions depend on which virtual environment tool was used. For instance, the {{ic|python}} command used in the Python 2 example points to {{ic|bin/python2.7}}, while the one in the {{ic|pyvenv}} example points to {{ic|bin/python3.5}}.
 +
 
 +
One major difference between {{ic|pyvenv}} and {{ic|virtualenv}} is that the former uses the system's Python binary by default:
 +
  $ ls -l pyvenv/bin/python3.5
 +
lrwxrwxrwx 1 foo foo 7 Jun  3 19:57 pyvenv/bin/python3.5 -> /usr/bin/python3
 +
The {{ic|virtualenv}} tool uses a separate Python binary in the environment directory:
 +
$ ls -l venv3/bin/python3.5
 +
lrwxrwxrwx 1 foo foo 7 Jun  3 19:58 venv3/bin/python3.5 -> python3
  
 
== Virtualenvwrapper ==
 
== 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.
+
''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]]. If you have not installed {{Pkg|python-virtualenv}} yet, {{Pkg|python-virtualenvwrapper}} will be installed now as a dependency.
+
[[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}}:
  export WORKON_HOME=~/.virtualenvs
+
 
  source /usr/bin/virtualenvwrapper.sh
+
  $ export WORKON_HOME=~/.virtualenvs
 +
  $ source /usr/bin/virtualenvwrapper.sh
 +
 
 +
If you are not using python3 by default (check the output of {{ic|$ python --version}}) you also need to add the following line to your {{ic|~/.bashrc}} prior sourcing the {{ic|virtualenvwrapper.sh}} script. The current version of the {{ic|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 {{ic|WORKON_HOME}} folder:
 
Re-open your console and create the {{ic|WORKON_HOME}} folder:
Line 52: Line 81:
  
 
* 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:  
  (my_env)$ deactivate
+
  (my_env) $ deactivate
  
 
== See Also ==
 
== See Also ==
*[http://pypi.python.org/pypi/virtualenv virtualenv Pypi page]
+
*[https://docs.python.org/3/library/venv.html Python venv (pyvenv)]
 +
*[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 02:31, 4 June 2016


virtualenv is a tool used to create an isolated workspace for a Python application. It has various advantages such as the ability to install modules locally, export a working environment, and execute a Python program in that environment.

Overview

A virtual environment is a directory into which some binaries and shell scripts are installed. The binaries include python for executing scripts and pip for installing other modules within the environment. There are also shell scripts (one for bash, csh, and fish) to activate the environment. Essentially, a virtual environment mimics a full system install of Python and all of the desired modules without interfering with any system on which the application might run.

Installation

Python 3.3+ comes with a tool called pyvenv and an API called venv for extending the native implementation. For applications that require an older version of Python, virtualenv must be used.

Packages

Install one of these packages from the official repositories to use a Python virtual environment.

Usage

All three tools use a similar workflow.

Creation

Use pyvenv or virtualenv to create the virtual environment within your project directory. Be sure to exclude the venv directory from version control--a copy of pip freeze will be enough to rebuild it.

pyvenv

This tool is provided by python (3.3+).

$ pyvenv venv

virtualenv

Use virtualenv for Python 3, available in python-virtualenv.

$ virtualenv venv

And virtualenv2 for Python 2, available in python2-virtualenv.

$ virtualenv2 venv

Activation

Use one of the provided shell scripts to activate and deactivate the environment. This example assumes bash is used.

$ source venv/bin/activate
(venv) $ which python
(venv) $ deactivate  # this is a shell function provided by bin/activate
$ which python

Normal Activities

Once inside the virtual environment, modules can be installed and scripts can be run as normal:

(venv) $ python -c 'import requests; print("this will not display because requests is not installed")'
(venv) $ pip install requests
(venv) $ python -c 'import requests; print("requests is now installed in the virtual environment")'

Python Versions

The binary versions depend on which virtual environment tool was used. For instance, the python command used in the Python 2 example points to bin/python2.7, while the one in the pyvenv example points to bin/python3.5.

One major difference between pyvenv and virtualenv is that the former uses the system's Python binary by default:

$ ls -l pyvenv/bin/python3.5
lrwxrwxrwx 1 foo foo 7 Jun  3 19:57 pyvenv/bin/python3.5 -> /usr/bin/python3

The virtualenv tool uses a separate Python binary in the environment directory:

$ ls -l venv3/bin/python3.5
lrwxrwxrwx 1 foo foo 7 Jun  3 19:58 venv3/bin/python3.5 -> python3

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