''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.

−

''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.

+

== Overview ==

−

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==

+

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.

−

''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.

+

== Installation==

−

== Virtualenv ==

+

[[Python]] 3.3+ comes with a module called ''venv''. For applications that require an older version of Python, ''virtualenv'' must be used.

−

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.

+

=== Packages ===

−

===Installation===

+

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

−

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

+

* Python 3.3+: {{pkg|python}}

−

# pacman -S python2-virtualenv

+

* Python 3: {{pkg|python-virtualenv}}

+

* Python 2: {{pkg|python2-virtualenv}}

−

===Basic Usage===

+

== Usage ==

−

An extended tutorial on how use ''virtualenv'' for sandboxing can be found [http://wiki.pylonshq.com/display/pylonscookbook/Using+a+Virtualenv+Sandbox here].

−

The typical use case is:

+

All three tools use a similar workflow.

−

* Create a folder for the new virtualenv:

−

$ mkdir -p ~/.virtualenvs/my_env

−

* Create the virtualenv:

−

$ virtualenv2 ~/.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 ==

+

=== Creation ===

−

''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.

+

Use ''venv'' or ''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.

−

===Installation===

+

==== venv ====

−

[[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.

+

{{Note|This method replaces the ''pyvenv'' script, which is deprecated since {{pkg|python}} 3.6.}}

+

+

This tool is provided by {{pkg|python}} (3.3+):

+

$ python -m venv venv

+

+

==== virtualenv ====

+

+

Use ''virtualenv'' for Python 3, available in {{pkg|python-virtualenv}}.

+

$ virtualenv venv

+

+

And ''virtualenv2'' for Python 2, available in {{pkg|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) $

+

+

Once inside the virtual environment, modules can be installed with ''pip'' and scripts can be run as normal.

+

+

To exit the virtual environment, run the function provided by {{ic|bin/activate}}:

+

+

(venv) $ deactivate

+

+

== 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 {{ic|bin/python2.7}}, while the one in the ''venv'' example points to {{ic|bin/python3.6}}.

+

+

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

''virtualenvwrapper'' allows more natural command line interaction with your virtual environemnts by exposing several useful commands to create, activate and remove virtual environments. This package is a wrapper for both {{Pkg|python-virtualenv}} and {{Pkg|python2-virtualenv}}.

+

+

=== Installation ===

+

+

[[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

export WORKON_HOME=~/.virtualenvs

source /usr/bin/virtualenvwrapper.sh

source /usr/bin/virtualenvwrapper.sh

+

+

Since python3 is a system-wide default in Arch, in order to be able to create python2 environments, you need to set {{ic|VIRTUALENVWRAPPER_PYTHON}} and {{ic|VIRTUALENVWRAPPER_VIRTUALENV}} prior to sourcing {{ic|virtualenvwrapper.sh}} in your {{ic|~/.bashrc}}:

+

+

export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python2.7

+

export VIRTUALENVWRAPPER_VIRTUALENV=/usr/bin/virtualenv2

+

+

The line {{ic|source /usr/bin/virtualenvwrapper.sh}} can cause some slowdown when starting a new shell. To fix this try using {{ic|source /usr/bin/virtualenvwrapper_lazy.sh}}, which will load virtualenvwrapper the first time a virtualenvwrapper function is called.

+

+

If you are not using python3 by default (check the output of {{ic|python --version}}) you need to add the following line to your {{ic|~/.bashrc}} prior sourcing the {{ic|virtualenvwrapper.sh}} script.

+

+

export 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:

$ mkdir $WORKON_HOME

$ mkdir $WORKON_HOME

+

{{Note|This seems to happen now automatically after re-open the console for the first time.}}

Latest revision as of 04:52, 29 December 2017

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.

Contents

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 module called venv. For applications that require an older version of Python, virtualenv must be used.

virtualenv

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) $

Once inside the virtual environment, modules can be installed with pip and scripts can be run as normal.

To exit the virtual environment, run the function provided by bin/activate:

(venv) $ deactivate

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 venv example points to bin/python3.6.

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

virtualenvwrapper

virtualenvwrapper allows more natural command line interaction with your virtual environemnts by exposing several useful commands to create, activate and remove virtual environments. This package is a wrapper for both python-virtualenv and python2-virtualenv.

Installation

Since python3 is a system-wide default in Arch, in order to be able to create python2 environments, you need to set VIRTUALENVWRAPPER_PYTHON and VIRTUALENVWRAPPER_VIRTUALENV prior to sourcing virtualenvwrapper.sh in your ~/.bashrc:

The line source /usr/bin/virtualenvwrapper.sh can cause some slowdown when starting a new shell. To fix this try using source /usr/bin/virtualenvwrapper_lazy.sh, which will load virtualenvwrapper the first time a virtualenvwrapper function is called.

If you are not using python3 by default (check the output of python --version) you need to add the following line to your ~/.bashrc prior sourcing the virtualenvwrapper.sh script.

export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3

Re-open your console and create the WORKON_HOME folder:

$ mkdir $WORKON_HOME

Note: This seems to happen now automatically after re-open the console for the first time.