Django in a virtualenv

9, Jul 2012


What is a Virtual Environment?

A quick way of working with multiple Python projects that have different dependencies is to use virtualenv. Virtualenv acts as a wrapper around Python, allowing you to create virtual environments in which you can install different Python modules, and even specify what version of the modules should be installed. Additional benefits of doing things this way is it becomes very easy to see what your dependencies are, allowing you to quickly install them on any other box you want to set up your project on.

In this example, we're going to setup Django to run in a virtual environment on Fedora or CentOS. We'll be working with a wrapper on this wrapper called python-virtualenvwrapper. It makes activating and managing your virtual environments much easier.

Setting the Environment

Firstly, we need to install virtualenvwrapper itself:

sudo yum install python-virtualenvwrapper

Once that completes there's a couple lines you've got to add to your ~/.bashrc file before it will work properly. Open ~/.bashrc in an editor and add the following lines into the file:


Then, source the file so that the changes are brought into your shell (source ~/.bashrc) - you'll see some text about how virtualenvwrapper is creating some files, and then you're good to go! You can now create a new virtual environment to hold your packages by running

mkvirtualenv <venv - the name of the environment>

You'll notice that once the command completes your prompt will now look something like this:

(<venv>)[user@host current_dir]

This serves to inform you that you're currently working within that environment. Anything you install through pip while in the virtual environment is only installed for that environment, and not any others, or accessible once you leave that environment. To do so, run the command deactivate - this will return you to your standard shell prompt. Always remember, any additional packages that you find yourself needing to bring into your Django application (or whatever it may be that you're building) remember to use pip to install them while the virtualenv is active.

To enter back into the environment issue the following command:

workon <venv>

Setting up for Django

I'm going to setup Django to run with mysql, so I'm going to need to install a couple more packages to provide that functionality. These modules have to be installed system wide, as they're dependencies for the python package MySQL-python which we will be installing through pip. To install mysql and its dependencies run the following command:

sudo yum install mysql mysql-server mysql-devel

Once that's finished, start mysqld, and run the mysql secure installation and follow its instructions:

sudo service mysqld start
sudo mysql_secure_installation

Installing Python Packages Through Pip

That's all the system wide configuration that needs to be done. It's now time to install Django in the virtual environment. If you're no longer in the virtual environment, make sure to reactivate it through the workon command. Once that's done, you can simply use pip install Django and the MySQL module for python:

pip install Django MySQL-python

Once pip has finished installing you should have access to the django-admin command; which shows that Django is installed and ready to run!

Tracking Application Requirements

An excellent feature of pip is that it is fully aware of every package that has been installed into the virtual environment. You can easily see a listing of packages by running the command pip freeze.

By sending the output of that command into a text file, we can create a requirements set that describes all the necessary packages for this particular project, allowing all the dependencies to be easily in stalled on a different system or environment:

pip freeze > requirements.txt

Then, when it comes time to setup a new installation on a different system all you need to do is create and activate a new virtual environment, and then after that finishes issue the command

pip install -r requirements.txt

Once that's complete, you're ready to run in the new environment!


shutupsquare said on May 18, 2014:

Hey thanks for this worked a treat. From a minimal install of centos-6.5 I had to install the EPEL repo and group install "Development Tools" to complete the above.

Have Something to Say?

Questions? Comments? Concerns? Let me know what you’re thinking.

  • You can use Markdown formatting here.