Home > python > using virtualenv (Part 1)

using virtualenv (Part 1)

Here I won’t explain what virtualenv is. There are lots of tutorials on this topic (see the “virtualenv” subsection here for instance, or this SO thread).

This post is about the basic usage of virtualenv.

Step 0 (20131029)
Before doing anything, create the file ~/.pip/pip.conf and edit it:

[install]
download-cache = ~/.pip/download_cache

This way each version is downloaded only once.

Update (20140807): Maybe using a cache is not that good idea. It happened to me that a new version came out of a package that I wanted to install but because of the cache, it installed an older version! I didn’t understand why I still had an out-of-date version. So I removed the ~/.pip folder entirely. I don’t recommend using a cache.

Installation
Install virtualenv:

sudo pip install virtualenv

Trying out
Create a directory where you want to store your project:

$ cd ~/python
$ mkdir mystuff
$ cd mystuff

Let’s create here a virtual environment:

$ virtualenv venv

You will get a subdirectory called “venv“, which contains a clean Python virtual environment. Activate it with the following command:

$ . venv/bin/activate

Notice that the prompt changes: now it includes the name of the virtual environment. If you call the command “python” or “pip“, the binaries in the virtual environment will be called.

Running example:

jabba@jabba-uplink:~$ cd python
jabba@jabba-uplink:~/python$ mkdir mystuff
jabba@jabba-uplink:~/python$ cd mystuff/
jabba@jabba-uplink:~/python/mystuff$ ls -al
total 16
drwxrwxr-x   2 jabba jabba  4096 Sep 21 15:58 .
drwxrwxr-x 234 jabba jabba 12288 Sep 21 15:58 ..
jabba@jabba-uplink:~/python/mystuff$ virtualenv venv
...
jabba@jabba-uplink:~/python/mystuff$ ls -al
total 20
drwxrwxr-x   3 jabba jabba  4096 Sep 21 15:58 .
drwxrwxr-x 234 jabba jabba 12288 Sep 21 15:58 ..
drwxrwxr-x   6 jabba jabba  4096 Sep 21 15:58 venv
jabba@jabba-uplink:~/Dropbox/python/mystuff$ . venv/bin/activate
(venv)jabba@jabba-uplink:~/Dropbox/python/mystuff$ which python
/home/jabba/Dropbox/python/mystuff/venv/bin/python                                                     
(venv)jabba@jabba-uplink:~/Dropbox/python/mystuff$ which pip
/home/jabba/Dropbox/python/mystuff/venv/bin/pip                                                               

To stop using a virtual environment, use the command “deactivate“.

Example
Say I have the package “requests” globally installed (with “sudo pip install requests“). When I launch the python shell, this module can be imported.

However, when you activate a clean virtual environment, globally installed 3rd party packages are not available. They must be installed with the virtual environment’s “pip” command, and they will be installed inside the “venv” directory. This is the real advantage of virtualenv: virtual environments are completely isolated from each other and you can install different package versions, there won’t be any conflict.

Running example:

jabba@jabba-uplink:~/Dropbox/python/mystuff$ which python
/usr/bin/python
jabba@jabba-uplink:~/Dropbox/python/mystuff$ python
Python 2.7.4 (default, Apr 19 2013, 18:28:01) 
[GCC 4.7.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
Persistent session history and tab completion are enabled.
>>> import requests
>>> 
jabba@jabba-uplink:~/Dropbox/python/mystuff$ . venv/bin/activate
(venv)jabba@jabba-uplink:~/Dropbox/python/mystuff$ which python
/home/jabba/Dropbox/python/mystuff/venv/bin/python
(venv)jabba@jabba-uplink:~/Dropbox/python/mystuff$ python
Python 2.7.4 (default, Apr 19 2013, 18:28:01) 
[GCC 4.7.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
Persistent session history and tab completion are enabled.
>>> import requests
Traceback (most recent call last):
  File "", line 1, in 
ImportError: No module named requests
>>> 
(venv)jabba@jabba-uplink:~/Dropbox/python/mystuff$ which pip
/home/jabba/Dropbox/python/mystuff/venv/bin/pip
(venv)jabba@jabba-uplink:~/Dropbox/python/mystuff$ pip install requests
Downloading/unpacking requests
  Downloading requests-1.2.3.tar.gz (348kB): 348kB downloaded
  Running setup.py egg_info for package requests
    
Installing collected packages: requests
  Running setup.py install for requests
    
Successfully installed requests
Cleaning up...
(venv)jabba@jabba-uplink:~/Dropbox/python/mystuff$ python
Python 2.7.4 (default, Apr 19 2013, 18:28:01) 
[GCC 4.7.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
Persistent session history and tab completion are enabled.
>>> import requests
>>> 
(venv)jabba@jabba-uplink:~/Dropbox/python/mystuff$

Where to put the project files
Store your project files (in this example) in the “~/python/mystuff” directory. If the virtual environment is activated, then the modules installed in the virtual environment are available for your project:

(venv)jabba@jabba-uplink:~/Dropbox/python/mystuff$ ls -al
total 24
drwxrwxr-x   3 jabba jabba  4096 Sep 21 16:16 .
drwxrwxr-x 234 jabba jabba 12288 Sep 21 15:58 ..
-rwxrw-r--   1 jabba jabba    53 Sep 21 16:16 hello.py
drwxrwxr-x   6 jabba jabba  4096 Sep 21 16:13 venv
(venv)jabba@jabba-uplink:~/Dropbox/python/mystuff$ cat hello.py 
#!/usr/bin/env python

import requests
print "hello"
(venv)jabba@jabba-uplink:~/Dropbox/python/mystuff$ ./hello.py 
hello

We installed “requests” in the previous step, and it’s nicely available for the project. If you uninstall requests from the virtual environment with “pip uninstall requests” and try to run hello.py, you will get an import error message.

In Part 2 we will see how to colorize the prompt and how to activate a virtual environment easily.

Categories: python Tags:
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: