Why Django?

…and whatever else I feel like writing about.

Virtualenv and Virtualenvwrapper

Virtualenv and virtualenvwrapper are two django toolkits that I wish that I would have learned when I first started using Django. Virtualenv is basically a virtual environment, that is very easy to set up. It allows you to maintain multiple projects that have different dependencies.
Since the articles about these toolkits are very well-written, and self-explanatory, I will simply refer you to those documents.
However, I do want to give you one word of advice, that I didn’t learn until after I spent several hours studying about these toolkits:
That is, you should really install virtualenvwrapper right after you install virtualenv–before you follow the instructions to call ‘virtualenv’ to create your virtual environment and ‘source bin/activate’ to activate your virtual environment. Then, rather than calling ‘virtualenv’ to create a new virtual environment, I would call ‘mkvirtualenv’. BE SURE TO CALL ‘mkvirtualenv’ with the ‘–no-site-packages’ option. If you do these two things in that order, you will not have to worry at all about modifying your PATH or your PYTHONPATH environment variables. This will all happen automatically.

Okay, so if you are new to Django, all of that probably went right over your head. That’s okay. It will all make sense after you go through these excellent articles. And it will make life a breeze for you as you add dependencies to your project, and especially when you are getting ready to deploy.

http://www.arthurkoziel.com/2008/10/22/working-virtualenv/
http://pypi.python.org/pypi/virtualenv
http://tumblr.intranation.com/post/766290325/python-virtualenv-quickstart-django
http://blog.sidmitra.com/manage-multiple-projects-better-with-virtuale
http://www.virtualenv.org/en/latest/http://www.doughellmann.com/projects/virtualenvwrapper/
http://www.doughellmann.com/docs/virtualenvwrapper/

To make things even more simple, since some of the articles above on virtualenv might have been written before virtualenvwrapper was available, I will give you the order that I installed and set these up in my environment. I am running on a Ubuntu 10 server, and I use Eclipse for my development environment.
I create all of my work projects in my ‘data’ directory right under root. I create a ‘envs’ directory inside ‘/data’, which is where all of my environment directories will be created. Inside each environment, I create a ‘projects’ directory where each project that will use that same environment will be created. You can organize your environment however you like. This is just what works for me.
Of course, you can skip the first command if you already have the setup tools (easy_install).

sudo apt-get install python-setuptools
sudo easy_install virtualenv
sudo easy_install virtualenvwrapper
export WORKON_HOME=/data/envs
mkdir -p $WORKON_HOME
cd $WORKON_HOME
mkvirtualenv --no-site-packages env1

Notice that ‘mkvirtualenv’ automatically activates that environment. To switch to a different environment, just call:
workon env2
where ‘env2’ is the name of the other environment.

Now, you can install whatever packages you will need in your new environment. For example:
easy_install django
easy_install django-dajaxice

At any point, you can call:
lssitepackages
This will give you a list of packages installed in your virtual environment.
After I create my virtual environment and install whatever packages I need, I then call:
cd $WORKON_HOME
mkdir projects
cd projects
django-admin.py startproject myapp

And when you are ready to startup your app:
python manage.py runserver 0.0.0.0:8000

Before using virtualenv, I noticed that some packages would not get installed into my site-packages directory.
Thus, my PYTHONPATH did not include these packages, and I saw all kinds of errors trying to use these libraries in my project.
If you are using Eclipse, you will need to create a new workspace that points to the projects directory in your new virtual environment.
Also, inside Eclipse, go to Window -> Preferences. Expand ‘Pydev’ and select ‘Interpreter – Python’. Click ‘New…’ and browse to your new python path.
For me, this was: ‘/data/envs/env1/bin/python’. Your list of libraries in your PYTHONPATH environment variable should now be listed under libraries. Click ‘OK’.
One more note. To deactivate, or exit an environment, just type:
deactivate
at the command prompt. I got this confused with:
source bin/activate
which now gets called automatically when you create the environment using ‘mkvirtualenv’, or when you switch to an environment using:
workon env2
If you have any further questions, the quickest way to get an answer (AFTER reading through the documentation, of course!) is by searching in the django users forum and submitting new questions if you do not find your answer.

HAPPY CODING!!!

Leave a comment