My first website was back in 1994 and honestly ever since then I always preferred to code everything the old fashion way — manually. My HTML code (and even when I moved into PHP) was always done by myself and when needed I created my own templates. I always shied away from frameworks because they required you to watch for bugs or security holes and had to consistently update. When I did it myself it just made it a bit easier for me to manage.
When you do stuff yourself, though, you do have limitations in coding that can be hard to overcome. Due to this I made the decision to start using Django for some of my websites — especially this blog. Django certainly made things easier in some ways, but also required me to stay on top of their versions and me needing to constantly keep my website code updated as well as the framework source files. In the process of doing this it was pretty hard to get the right type of information I needed to do things. The most important component I needed to be able to support was running multiple versions of Django on my Macintosh. To do this I decided the best way was to use a program called - Virtualenv.
What is Virtualenv? Virtualenv is a tool that is used to create isolated Python environments. Each of these virtual environments have their own installation directories, libraries, application links, etc. It really is the best practice recommended way to manage development of frameworks like Django and their dependencies. You can use virtualenv on your Unix based OS like Ubuntu, OS X, Debian, etc. The key reason you set up virtualenv is because it helps you keep your system package directories safe. You can even set up different Python versions if you need to.
You will find a bunch of tutorials out there about how to set up virtualenv, Django, etc., for a development environment. Here are the steps I used below simplified to the fewest amount possible.
First thing you need to do is install your python-virtualenv package files. We are using the SUDO command because we want these programs installed system wide.
OS X: sudo easy_install pip OS X: sudo pip install virtualenv Ubuntu: sudo apt-get install python-virtualenv
Note that if you do not have MySQL installed you should do this as well if you are going to need a database connection for your application. I installed mine system wide because it can work that way just fine. I am not going into installing MySQL in this blog post because of technical specifics of it.
Next you will have to create a folder in your home directory to store the virtual environment python files and libraries as well as your Django project files. Do this without a SUDO command because you want the folders to have write permissions by your account and not be set up as root access.
mkdir ~/djangoenv cd ~/djangoenv
Again I want to state that it is important that you do not use SUDO in any of the commands going forward because you need to ensure that the files are created in the local home folders and not put into your ROOT System Package folders. When you do a SUDO it causes the system to place stuff in your system packages which will cause all types of problems. If you created the folder in your home directory correctly it should have permissions where your local account can access it and you shouldn’t need to sudo or anything as root.
In many tutorials they state that you must have PIP installed in each virtual environment. But, virtualenv will install PIP so you can skip adding it. Now we need to create virtual environments for each version of Django we want to develop for. In this example I will use the latest version and a previous one — Django 1.7.1 and 1.6.8. So let’s create virtualenv’s for each version.
virtualenv django168 virtualenv django171
Virtualenv will create the directories and install the relevant Python libraries, PIP, etc. You should see both a django168 and django171 folder under your djangoenv folder.
Next, let’s activate each environment one at a time and install the version of Django that is relevant to that directory.
source django168/bin/activate pip install django==1.6.8 [. . . . wait for the installation to complete . . . .] deactivate sourece django171/bin/activate pip install django==1.7.1 [. . . . wait for the installation to complete . . . .] deactivate
The “source folder/bin/activate” is the command that starts your virtualenv. You will know that it worked because your shell command line will start with the folder name in paranthese. [e.g. - (django168)MyComputer:djangoenv myusername$ ]. The “deactivate” command exits the virtualenv shell. If you wish to install different versions of Django just put in the appropriate version number.
Why don’t we check that you have set everything up correctly. Let’s verify the version of Python and Django you have installed in each virtualenv.
source django168/bin/activate python [. . . . make note of the version of python running . . . .] import django django.VERSION [. . . . make note of the version of django running . . . .] deactivate sourece django171/bin/activate python [. . . . make note of the version of python running . . . .] import django django.VERSION [. . . . make note of the version of django running . . . .] deactivate
If everything was done correctly you should see a different version of Django running in each virtualenv. If you don’t, likely you used the SUDO command and it installed it system wide. If so you need to uninstall Django (pip uninstall django) and do it again like I explained.
Next you just need to create a folder to house your project files for each Django project. You can put these folders under each of the virtualenv folders or in another structure if you want to. Personally, I prefer to keep them in a different folder than the virtualenv directory and I have a small text file in each project with a name based on the Django version it was coded under.
Here is an example of my directory tree. Note the files called django171.txt and django168.txt.
/Users/username/djangoenv |— django168 | |— bin | |— include | `— lib |— django171 | |— bin | |— include | `— lib `— websites |— website1 | |— django171.txt | |— website1 | |— static | `— manage.py |— website1_ver1 | |— django168.txt | |— website1 | |— static | `— manage.py `— website2 |— django171.txt |— website2 |— static `— manage.py
So basically I just start my virtualevn for the version of Django I want to run, then CD into the correct website folder and then start the Django development web server.
python manage.py runserver
Problems? If you have problems go back through my steps and make sure you followed everything specifically. Do note that sometimes things can be a bit weird if you had messed with your system before. I can confirm these steps worked for me because I set up a completely new Ubuntu virtual machine on VirtualBox and followed these steps with no issues. Additionally, I did run them on my Mac and it is working.
For the Mac OS X version I did initially have some problems because after an OS upgrade it seemed to screw with my packages and stuff. So I was unable to create a new virtualenv or anything. I was able to fix the problem by running a specific set of commands that basically seemed to reset the links back to the correct packages. Then I was able to create a new virtualenv instance, use PIP, and install Django into it and link it to my MySQL database. I cannot guarantee this will work on your Macintosh or is the best way to do it, but give it a shot. Here are the commands I used.
sudo easy_install -U pip sudo easy_install -U virtualenv sudo easy_install -U mysql-python
Once I updated the above packages it seemed to allow me to create a new virtual environment instance as well as linked my system install of MySQL to it. I did run the mysql-python command after I had created a new virtualenv for the latest Django version.
If you have questions or problems or if this tutorial didn’t answer what you were looking for please contact me.