virtualenv. The intention behind the use of
virtualenvas 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
virtualenvon Debian Linux is simple as:
# apt-get install python-virtualenvAt this stage we are able to create our fisrt
virtualenvpython development environment eg.
$ 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-packagesoption we ensure that no system global packages will be linked to our new
virtualenvenvironment. Let's explore in more detail what just happened. Firstly,
virtualenvhas created a new directory to act as a container for our
$ 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
virtualenvenvironment 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
$ cd myapp/ $ source bin/activate (myapp)debian@hostname:~/myapp$One you enter
virtualenvpython environment your PATH to binary executables will change:
$ echo $PATH /home/debian/myapp/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/gamesAs a result all your python commands will be executed using by using
virtualenvenvironment binaries. Use
whichcommand 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_installNow, we are ready to install packages without affecting the global system wide environment. As an example we can install
djangopackage local to only our
(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
(myapp)debian@hostname:~/myapp$ python -c "import django; print(django.get_version())" 1.8All is working as expected. Once you finish your work with
virtualenvenvironment you can exit by using
deactivatefunction provided within
(myapp)debian@hostname:~/myapp$ deactivate debian@hostname:~/myapp$An attempt to access
djangopackages installed within
virtualenvenvironment 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