I call it, virtual envy....

Virtualization is a development strategy for isolating your applications in virtual containers on your server or in the local development environment. Your applications most likely have many different dependencies and requirements. By isolating these app settings in their own virtual container, you don't have to worry about blowing up another application because you installed an update or removed a package that relies on one of those many dependencies.

You definitely do not want the packages in your default Python (or Anaconda) path to conflict with each other. This can cause serious headaches down the road when you begin building new Django apps in future projects.

In order to maintain project and application separation, we have an environment tool that keeps our projects isolated in their own separate virtual environments.

It's called, virtualenv.

The Hitchhiker's Guide to Python also has a nice virtualenv tutorial.

This can be used on any platform. I am using Ubuntu 14.04.3 for my Django development. (edit, I just installed Ubuntu Mate 15.10 for a new project VM).

Let's get started with virtualenv by creating a new working directory in our ~/home folder. From our Ubuntu terminal, let's install virtualenv from the package index.

>$ pip install virtualenv

Next, let's setup our project folders...

>$ mkdir virtualenvs
>$ mkdir django-code-library

Our new virtual environment will live in the folder:

~/home/virtualenvs and our Django project will live at ~/home/django-code-library. Change the directory to ~/home/virtualenvs and enter the following...

>$ virtualenv myenv --python=python3

The necessary files will be installed and a new virtual environment is created at ~/home/virtualenvs/myenv/

Now, we need to activate our virtual environment.

>$ SOURCE myenv/bin/activate

Once the virtual environment has been activated, the prompt will change, like so...

(myenv) >$ pip install django
(myenv) >$ pip install django-rest-framework
(myenv) >$ pip install django-registration-redux
(myenv) >$ pip install django-bootstrap
(myenv) >$ pip install django-crispy-forms
(myenv) >$ cd ~/home/django-code-library
(myenv) > ~/home/django-code-library $ django-admin startproject django1 

Notice the (myenv) before the prompt. This shows you the virtual environment is activated and ready to use.

We also installed several required project dependencies and started up our sample website project, django1

To deactivate the virtual environment, at the prompt, enter:

(myenv) >$ deactivate

On Windows-based machines, please note that the activation script is located at:

> myenv\scripts\activate

And finally, let's check our virtualenv for the Django version and which Python is answering our calls.

(myenv) ~/home/django-code-library $ python -c "import django; print(django.get_version())"
(myenv) ~/home/django-code-library $ which python
~/home/virtualenvs/myenv/bin/python: Python 3.4.3

We'll begin creating our apps in the next chapter.

Craig Derington

Secular Humanist, Libertarian, FOSS Evangelist building Cloud Apps developed on Red Hat Enterprise Linux and Ubuntu Server. My toolset includes Python, Celery, Flask, Django, MySQL, MongoDB and Git.

comments powered by Disqus