Python Virtual Environments

Installing and using Python Virtual Environments is a recommended practice to separate your different Python environments with its own package requirements and your default Python interpreter.

  • Installing Virtualenv
$ pip install virtualenv

If after installing virtualenv you see something like this:
You are using pip version 8.1.2, however version 9.0.1 is available.
You should consider upgrading via the ‘pip install –upgrade pip’ command.

Upgrade pip per instructions.

$ pip install --upgrade pip

Update pip packages if needed.

$ pip list --outdated
mercurial (3.5+20150731) - Latest: 4.0.0 [sdist]setuptools (23.1.0) - Latest: 28.8.0 [wheel]
$ pip freeze --local | grep -v '^\-e' | cut -d = -f 1  | barges -n1 pip install -U
  • Creating a virtual environment

First create a folder to hold your virtual environments.

$ mkdir ~/Virtualenvs
$ cd ~/Virtualenvs

Then create a virtual environment.

$ virtualenv venv-django

The virtual environment will be created with the default Python interpreter.

$ which python
/usr/local/bin/python

$ python -V
Python 2.7.12

$ django/bin/python –V
Python 2.7.12

Virtualenv can also be instructed to use a different Python interpreter.

$ virtualenv -p /usr/local/bin/python3 venv-django
Running virtualenv with interpreter /usr/local/bin/python3
Using base prefix '/usr/local/Cellar/python3/3.5.2_3/Frameworks/Python.framework/Versions/3.5'
New python executable in /Users/dummyuser/Virtualenvs/venv-django/bin/python3.5
Also creating executable in /Users/dummyuser/Virtualenvs/venv-django/bin/python
Installing setuptools, pip, wheel...done.
  • Activating a virtual environment
$ source venv-django/bin/activate
(django) deb-comp:Virtualenvs dummyuser$

Leaving the activate virtual environment to go back to your default Python interpreter.

$ deactivate
  • Removing a virtual environment

Just remove the directory that was created with virtualenv.

$ rm -rf venv-django
  • Restricting pip

Avoid executing pip if there is no active virtual environment, this step is optional but in this way you’ll be sure that you are installing packages under an active virtual environment and not globally.
Add the following to your ~/.bashrc:

export PIP_REQUIRE_VIRTUALENV=true

Make changes effective under your shell session.

$ source ~/.bashrc

If the changes are applied correctly and there is no active virtual environment, then when you execute pip you will see a message like this:

$ pip install requests
Could not find an activated virtualenv (required).

For convenience, if you want to install packages globally you can add to your ~/.bashrc the following:

gpip() {
    PIP_REQUIRE_VIRTUALENV="" pip "[email protected]"
}

Source your ~/.bashrc and then you can install pip packages with gpip without restrictions.

References:

http://docs.python-guide.org/en/latest/dev/virtualenvs/
http://docs.python-guide.org/en/latest/dev/pip-virtualenv/
https://hackercodex.com/guide/python-development-environment-on-mac-osx/
http://stackoverflow.com/a/3452888