What is a virtualenv, and why should I use one?

Solution 1:

Running with the system Python and libraries limits you to one specific Python version, chosen by your OS provider. Trying to run all Python applications on one Python installation makes it likely that version conflicts will occur among the collection of libraries. It's also possible that changes to the system Python will break other OS features that depend on it.

Virtual environments, or "virtualenvs" are lightweight, self-contained Python installations, designed to be set up with a minimum of fuss, and to "just work" without requiring extensive configuration or specialized knowledge.

virtualenv avoids the need to install Python packages globally. When a virtualenv is active, pip will install packages within the environment, which does not affect the base Python installation in any way.

In Python 3.3 or later, you can create a virtualenv as follows:

$ python3 -m venv ENV_DIR

For Windows, you should replace python3 with the full path to python.exe:

>C:\Python34\python.exe -m venv ENV_DIR

(This is a typical Python installation; your system may vary.)

In older versions of Python, including Python 2, one of the following commands should work in most cases:

$ virtualenv ENV_DIR
$ venv ENV_DIR
$ pyvenv ENV_DIR
$ pyvenv3 ENV_DIR

ENV_DIR should be a non-existent directory. The directory can have any name, but to keep these instructions simple, I will assume you have created your virtualenv in a directory called venv (e.g. with python3 -m venv ./venv).

To work in your virtualenv, you activate it:

$ . ./venv/bin/activate
(venv)$ 

Or use this if you have a windows system:

$ venv\Scripts\activate

The (venv) in the shell prompt lets you know which virtualenv you have activated, but you can turn this feature off if you do not like it. You can run all the usual Python commands, and they will be local to your virtualenv:

(venv)$ pip install requests numpy
[...]
(venv)$ python
[...]
>>> import requests
>>> import numpy as np
>>> 

python will run the version of Python that you installed into your virtualenv, so (for example) you don't have to type python3 to get Python 3. The Python that it runs will have access to all the standard library modules and all the packages you installed into the virtualenv, but (by default) none of the packages installed in the system-wide site-packages directory.

This last rule is important: by restricting your virtualenv to only use locally-installed packages, you can ensure that you control exactly which dependencies your project is using, even if some new system-wide package gets installed or updated next week. If you like, you can get a listing of your installed packages:

(venv)$ pip freeze
requests==2.13.0
numpy==1.12.0
(venv)$ 

pip can also parse this format and install from it, and it will install the same versions, even if updates have been released in the meantime:

(venv)$ pip freeze >requirements.txt

(some-other-venv)$ pip install -r requirements.txt
[...]
(some-other-venv)$ python
>>> import requests
>>> import numpy as np
>>> 

You can get out of the virtualenv by deactivating it:

(venv)$ deactivate
$ python
[...]
>>> import requests
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named 'requests'

You can create as many virtualenvs as you like, and they won't interfere with each other, nor with your system packages. A virtualenv is "just" a directory with a bunch of binaries and scripts under it, so you can remove a virtualenv the same way you remove any directory (rm -r venv on Unix). If the virtualenv is activated when you remove it, you may confuse your shell, so it's probably a good idea to deactivate first in that case.

Solution 2:

Some times you are not given root privileges and you might end up not being able to use sudo. Many other times, it's not advisable to use sudo to install packages as it might overwrite some package which might be in use by some other applications.

Virtualenv can help you create a separate environment where you don't need root privileges as well as be able to tailor the environment according to your need. It consists of self-contained python installation which only interacts with your specific created environment.

So basically, it gives you a bit of freedom as well as avoid damaging (or modifying) the root environment which might be hosting many old functionalities of old applications.

Installation is pretty easy too.

Solution 3:

Installing packages with sudo pip will install packages globally, which may break some system tools.

By install globally it means you will install your packages in place like /usr/lib/python2.7/site-package so if some packages need a previous version of your python packages, this action may break it.

virtualenv allows you to avoid installing Python packages globally by making an isolated python environment. That means it will install packages just in your desire project folder.

On mac and linux

  • Install

    python3 -m pip install --user virtualenv
    
  • Creating a Virtual Env: Go to your desired project folder

    python3 -m virtualenv env
    
  • Activating a virtualenv: In your desired project folder

    source env/bin/activate
    

After activating you can install your packages using pip.

For more information about using it in Windows: How to use virtualenv in Windows