Python allows you to install packages using PIP. This works fine for small projects, but when you want to create a portable application you might run into problems if your application depends on a version of a package that is different to the version that another application depends on. Because by default PIP will install packages in a folder similar to:

1
/usr/lib/python2.7/site-packages/

A folder will be created for the installed library. For example, when I installed flask, it was installed under:

1
/usr/lib/python2.7/site-packages/flask/

As you can see, the library folder is not versioned, so if you install a new version, the older will be overwritten.

Another problem with installing files in a location like /usr/lib/python2.7/site-packages/ is that you need to use sudo to do it. If you don’t have sudo privileges in your current system, you wont be able to install packages (There are other ways around this, but virtual environments is the most elegant).

Installation

1
sudo pip install virtualenv

Usage

Create a folder where your project will live and create a virtual environment:

1
2
3
mkdir ~/python-project
cd ~/python-project
virtualenv env

This creates a copy of python in ~/python-project/env/ that will be used to run python-project. This effectively isolates the version of python(and all its dependencies) from the ones globally installed.

Now that the environment is installed, we need to activate it to start using it:

1
. env/bin/activate

This changes the prompt to something like this:

1
(env)[adrian@localhost python-project]$

Once in the virtual environment, installing packages using PIP will do it only for the current project, and sudo won’t be necessary:

1
pip install flask

To go out of the virtual environment you need to deactivate it:

1
deactivate

If you are curious, you can see that the package we installed is now here:

1
~/python-project/env/lib/python2.7/site-packages/flask/

Develop using a virtual environment

Now that we have an environment with our dependencies, we need to use it. Since I’m using flask for my example I’m going to create a sample flask app in ~/python-project/app.py

1
2
3
4
5
6
7
8
9
from flask import Flask
app = Flask(__name__)

@app.route("/")
def hello():
    return "Hello World!"

if __name__ == "__main__":
    app.run()

If you don’t have flask installed globally and you try to run the app you will get an error:

1
2
3
4
5
python app.py
Traceback (most recent call last):
  File "app.py", line 1, in <module>
    from flask import Flask
ImportError: No module named flask

If you activate your environment first you will be able to run your app without the need to install flask globally.

1
2
. env/bin/activate
python app.py

This time your app is executed using only the packages on your virtual environment.

[ programming  python  ]
Introduction to PIP
Writting Django applications for your project
Setting up a Django work environment
Introduction to Object Oriented Programing in Python
Creating functions with Python