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/
myapp/
├── bin
│ ├── activate
│ ├── activate.csh
│ ├── activate.fish
│ ├── activate_this.py
│ ├── 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
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 (myapp)debian@hostname:~/myapp$
One you enter virtualenv python environment your PATH to
binary executables will change:
$ echo $PATH /home/debian/myapp/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games
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 /home/debian/myapp/bin/python /home/debian/myapp/bin/pip /home/debian/myapp/bin/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; print(django.get_version())" 1.8
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 debian@hostname:~/myapp$
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