Python virtual environments are used to create isolated python environments to avoid dependency and version conflicts, and also indirectly take care of permission issues. But what is the easiest way to set it up, and use it, in Ubuntu?
Python virtual environments are used to create isolated python environments to avoid dependency and version conflicts, and also indirectly take care of permission issues. But what is the easiest way to set it up, and use it, in Ubuntu?
With
virtualenvwrapper
(user friendly wrappers for the functionality ofvirtualenv
)Install virtualenv
Install
virtualenv
withInstall virtualenvwrapper
The reason we are also installing virtualenvwrapper is because it offers nice and simple commands to manage your virtual environments. There are two ways to install
virtualenvwrapper
:As Ubuntu package (from Ubuntu 16.04)
Run
then run
Using pip
Install and/or update pip
Install pip for Python 2 with
or for Python 3
(if you use Python 3, you may need to use
pip3
instead ofpip
in the rest of this guide).Optional (but recommended): Turn on bash autocomplete for pip
Run
and run
source ~/.bashrc
to enable.Install virtualenvwrapper
Because we want to avoid
sudo pip
we installvirtualenvwrapper
locally (by default under~/.local
) with:and
Source virtualenvwrapper in
.bashrc
Setup virtualenv and virtualenvwrapper:
First we export the
WORKON_HOME
variable which contains the directory in which our virtual environments are to be stored. Let's make this~/.virtualenvs
now also create this directory
and put this export in our
~/.bashrc
file so this variable gets automatically definedWe can also add some extra tricks like the following, which makes sure that if
pip
creates an extra virtual environment, it is also placed in ourWORKON_HOME
directory:Source ~/.bashrc to load the changes
Test if it works
Now we create our first virtual environment. The
-p
argument is optional, it is used to set the Python version to use; it can also bepython3
for example.You will see that the environment will be set up, and your prompt now includes the name of your active environment in parentheses. Also if you now run
you should see a lot of
/home/user/.virtualenv/...
because it now doesn't use your system site-packages.You can deactivate your environment by running
and if you want to work on it again, simply type
Finally, if you want to delete your environment, type
Enjoy!
Thanks to the author of this blogpost.
Virtual environments offer a way for managing and isolating dependencies on a per-project basis. Moreover, they also avoid the whole
sudo pip install
situation, which is a security risk as I have explained in https://askubuntu.com/a/802594/15003. The official Python documentation also encourages the use of virtual environments.The easiest way to create and use virtual environments for both Python 2 and Python 3 is to install
virtualenv
usingapt
orapt-get
. For each Python project, create a virtualenv and then activate it. Note that the virtualenv is specific for a particular Python version. After activation, usepip
to install Python packages as usual regardless of whether you are using Python 2 or 3; there is no need to usepip3
for Python 3.sudo
is only used to installvirtualenv
and is not used withpip
, therefore avoiding the aforementioned security risk. The commands to do so are:If you would like to create a virtualenv for Python 3, replace
virtualenv venv
with:Read more about various bells and whistles for
virtualenv
at https://virtualenv.pypa.io/en/stable/.It's easy, you install python-virtualenv. Then you can create a virtualenv with the
virtualenv
command. See their documentation for more.With the venv module available from Python 3.3 (and Ubuntu 15.10)
Virtual environments (venvs) are so popular that the functionality is now included in python itself (from 3.3 onwards). To use it on Ubuntu you need to install
python3-venv
(since theensurepip
module is not available):After that you can use
to create a virtual environment called
myvirtualenv
. You can then useto activate the virtual environment. To deactivate simply type
EDIT: The
pyvenv
script has been deprecated in favour ofpython3 -m venv
. This prevents confusion as to what Python interpreterpyvenv
is connected to and thus what Python interpreter will be used by the virtual environment. (source)