1$ pip install virtualenv 2$ virtualenv /path/to/django_project/ 3$ . /path/to/django_project/bin/activate 4$ pip install django
I hang around a fair bit in #django now on IRC. It’s open most of the time I am at work: if I am waiting for something to deploy, I’ll keep an eye out for someone that needs a hand, or whatever. Yesterday, I attempted to help someone out with an issue with django and apache: I ended up having to go home before it got sorted out.
One of the things that came up was how to actually install django. The person was following instructions on how to do so under Ubuntu, but they weren’t exactly ‘best practice’.
One of the things I wish I had been around when I first started developing using python is
virtualenv. This tool allows you to isolate a python environment, and install stuff into it that will not affect other virtual environments, or the system python installation.
Unfortunately, it does not come standard with python. If it were part of the standard library, it may reduce the likelihood of someone not using it. The upside of it not being in the standard library is that it gets updated more frequently.
First, see if virtualenv is installed:
1$ virtualenv --version
If not, you’ll need to install it. You can install it using
easy_install, if you have either of those installed. If you are a super-user on your machine (ie, it is your computer), then you may want to use sudo. You can have it installed just in your user account, which you might need to do on a shared computer.
You’ll probably also want to install pip at the system level. I do this first, and use it to install virtualenv, fabric and other packages that I need to use outside of a virtualenv (mercurial springs to mind). Do note that a virtualenv contains an install of pip by default, so this is up to you: once you have virtualenv installed, you can use pip in every virtualenv to install packages.
Setting up a virtual environment
I recommend using virtualenv for both development and deployment.
I think I use virtualenv slightly differently to most other people. My project structure tends to look like:
1/home/user/development/<project-name>/ 2 bin/ 3 fabfile.py 4 include/ 5 lib/python2.6/site-packages/... 6 project/ 7 # Project-specific stuff goes here 8 src/ 9 # pip install -e stuff goes here 10 tmp/
Thus, my $VIRTUAL_ENV is actually also my $PROJECT_ROOT. This means that everything is self contained. It has the negative side-effect of meaning if I clone my project, I need to install everything again. This is not such a bad thing, as I use Fabric to automate the setup and deployment processes. It takes a bit of time, but using a local pypi mirror makes if fairly painless.
Obviously, I ignore
lib/ and the other virtualenv created directories in my source control.
However, since we are starting from scratch, we won’t have a fabfile.py to begin with, and we’ll just do stuff manually.
1$ cd /location/to/develop 2$ virtualenv my_django_project
That’s it. You now have a virtual environment.
Installing django/other python packages
You’ll want to activate your new virtualenv to install the stuff you will need:
1$ cd my_django_project 2$ . bin/activate 3(my_django_project)$
Notice the prompt changes to show you are in a virtual environment.
Install the packages you need (from now on, I’ll assume your virtualenv is active):
1 $ pip install django
There has been some discussion about having packages like psycopg2 installed at the system level: I tend to install everything into the virtualenv.
So that’s it. You now have django installed in a virtual environment. I plan to write some more later about my deployment process, as well as how I structure my django projects.