In this config we will setup a python virtualenv. The intention behind the use of virtualenv as a python development environment is to separate global system installed executables and libraries with packages installed directly inside virtualenv. As a result, one is able to test a given python application with multiple system environments on a single host.
The installation of virtualenv on Debian Linux is simple as:
# apt-get install python-virtualenv
At this stage we are able to create our fisrt virtualenv python development environment eg. myapp
$ virtualenv --no-site-packages myapp
Running virtualenv with interpreter /usr/bin/python2
New python executable in myapp/bin/python2
Also creating executable in myapp/bin/python
Installing setuptools, pip...done.
By using the --no-site-packages option we ensure that no system global packages will be linked to our new virtualenv environment. Let's explore in more detail what just happened. Firstly, virtualenv has created a new directory to act as a container for our myapp development.
$ tree -L 2 myapp/
├── bin
│   ├── activate
│   ├── activate.csh
│   ├──
│   ├──
│   ├── easy_install
│   ├── easy_install-2.7
│   ├── pip
│   ├── pip2
│   ├── pip2.7
│   ├── python -> python2
│   ├── python2
│   └── python2.7 -> python2
├── lib
│   ├── python2.7
│   └── python-wheels
└── local
    ├── bin -> /home/debian/myapp/bin
    └── lib -> /home/debian/myapp/lib

7 directories, 12 files

Subscribe to our NEWSLETTER and receive latest Linux news, jobs, career advice and tutorials.

As you can see our new virtualenv environment contains all necessary tools to shape our environment such as separate python, pip and easy_install binaries.

At this stage we are ready to enter virtualenv environment:
$ cd myapp/
$ source bin/activate
One you enter virtualenv python environment your PATH to binary executables will change:
$ echo $PATH
As a result all your python commands will be executed using by using virtualenv environment binaries. Use which command to locate your a full path to your python binaries:
(myapp)debian@hostname:~/myapp$ which python pip easy_install
Now, we are ready to install packages without affecting the global system wide environment. As an example we can install django package local to only our myapp environment:
(myapp)debian@hostname:~/myapp$ pip install django
Downloading/unpacking django
  Downloading Django-1.8-py2.py3-none-any.whl (6.2MB): 6.2MB downloaded
Installing collected packages: django
Successfully installed django
Cleaning up...
Test django package installation:
(myapp)debian@hostname:~/myapp$ python -c "import django; 
All is working as expected. Once you finish your work with virtualenv environment you can exit by using deactivate function provided within bin/activate script:
(myapp)debian@hostname:~/myapp$ deactivate 
An attempt to access django packages installed within virtualenv environment will now fail:
$ python -c "import django; print(django.get_version())"
Traceback (most recent call last):
  File "", line 1, in 
ImportError: No module named django
Submit your RESUME, create a JOB ALERT.
Subscribe to NEWSLETTER and receive latest news, jobs, career advice and tutorials.
Get extra help by visiting our LINUX FORUM or simply use comments below.