So, you learned some Python, heard a thing or two about Django framework and decided to try it out. You’re in luck, because you can set up your development environment in less then 60 seconds.
The idea is that you install and run Django within virtualenv. If you wonder what is virtualenv and why you should use it, the answer is simple. Virtualenv is a tool to create isolated Python environments. If you develop your Python application within virtual environment, you don’t have to worry about bloating your system with various Python modules or choosing between AppA which requires PythonLib 1.0 and AppB which requires PythonLib 2.0. With virtualenv, you have clean Python environment in which you can install whatever Python module you’d like.
Installing Virtualenv
Before installing virtualenv, you’ll need to install some packages first. So, open your terminal and execute the following command:
$ sudo apt-get install python-setuptools python-dev build-essential
Now, when you have all the necessary tools to continue, you’ll need to install a better Python package installer called pip. Ironically, to install pip you’ll use easy_install
, the tool that pip is going to replace. To install pip, all you need to do is run the following command:
$ sudo easy_install -U pip
You are now ready to install virtualenv:
$ sudo pip install -U virtualenv
Creating a virtual environment
Setting up virtual environment is as easy as installing virtualenv. Open your terminal, cd to the directory where you plan to create virtual environment and run the next command:
$ virtualenv --no-site-packages my-django-env
The --no-site-packages
switch instructs virtualenv not to give newly created virtual environment access to the global site-packages directory. Of course, instead of my-django-env
you can use any other name for destination directory.
To activate virtualenv, you simply enter:
$ source my-django-env/bin/activate
or if you want to save couple of miliseconds on typing:
$ . my-django-env/bin/activate
When you activate virtualenv, your prompt should change to something like this:
$ (my-django-app)user@host:~/path/to/virtualenv$
To deactivate virtualenv, just enter:
$ deactivate
Installing Django
Now when you learned how to get in and out of virtualenv, you’re ready for installing Django module. The easiest way to do that is to run the following command in terminal:
$ pip install -E my-django-env Django
-E
switch tells pip to install Django in my-django-env virtual environment. Logically, if you activate your virtualenv, you can run pip install command without mentioned switch:
$ pip install Django
Moving application around
Let’s say that you’d like to copy your Django instance to another computer or server and you don’t recall all the packages you installed in virtual environment while you developed your application. To avoid head-scratching and hair loss, you can export all currently installed packages (exact versions) to a file:
pip freeze -E my-django-env > modules.txt
And then you can install modules in virtual environment on the other computer with:
$ pip install -E my-django-env -r modules.txt