pip and virtualenv

Python comes with a lot of great features built in but heaps more can be installed from the Python Package Index. Using the command pip this is really simple. To install for example the requests library you write the following from a terminal / command prompt:

pip install requests

To list all packages installed write:

pip freeze

Example output:



When working on multiple Python projects on the same computer you will sooner or later run inte package dependency problems. One project might require requests version 1 while another requires requests version 2.


A solution to this problem is using virtualenv to create virtual environments. When a virtual environment is created it looks like a brand new Python installation. Calling pip freeze will show that no packages are installed. If a package is installed it is available as long as the environment is active.

To make virtualenv easier to use there is a project called virtualenvwrapper which adds a few commands to the terminal. If using Windows virtualenvwrapper-win should be used but the commands are the same.


To create a new virtual environment (replace test with the name you want):

mkvirtualenv test

The environment will be created, your Python installation will be available and the new environment will be activated.

It will look like this:

Computer:~ marcus$ mkvirtualenv test
New python executable in test/bin/python3.4
Also creating executable in test/bin/python
Installing setuptools, pip, wheel…done.
(test)Computer:~ marcus$

Notice how the prompt has changed from  now starting with (test). This is to inform which environment is active.

To stop using the environment:


The prompt changes back to Computer:~.

To use the newly created environment again:

workon test

If you don’t need your environment any more you can easily delete it:

rmvirtualenv test

Notice that you can not remove an environment that is currently active, you will have to deactivate it first.


All at once

Install multiple dependencies at once using a requirements file. At its simplest level a requirements file is just a text file listing a dependency name per line but usually also contains the version like the output of pip freeze.

pip install -r requirements.txt

Multiple Python versions

Even though Python 3 is the recommended version to use, there are cases when the much older Python 2 is required. If more than one Python version is installed the requested one can be chosen when creating the virtual environment.

mkvirtualenv test --python=/usr/local/bin/python2


  1. Python Package Index
  2. pip
  3. VirtualEnv
  4. VirtualEnvWrapper
  5. VirtualEnvWrapper Win

Lämna ett svar

Din e-postadress kommer inte publiceras. Obligatoriska fält är märkta *