Edgar Latorre

I'm Edgar and this is my blog where I write about software development.

Python 3.0, Django 1.5 and Heroku

17 Jul 2013

Hi folks, today I’m going to write about how to use python 3 and django on heroku.

First of all, we’ll install virtualenv to control the python’s version that we’ll use:

sudo pip install virtualenv

Particularly I prefer to use the virtualenvwrapper, then let’s install the virtualenvwrapper. If you don’t know this wrapper see the documentation: http://virtualenvwrapper.readthedocs.org/en/latest/

To install the virtualenvwrapper is easy:

sudo pip install virtualenvwrapper

Now we need to make a directory for our environments and to set it up as a global variable:

export WORKON_HOME=~/envs #set the directory envs as a workon_home.
mkdir $WORKON_HOME #make the directory envs on your home.
source /usr/local/bin/virtualenvwrapper.sh

After the virtualenv and virtualenvwrapper are installed, we need to download the python and to install it:

wget http://python.org/ftp/python/3.3.2/Python-3.3.2.tgz
./configure
make
sudo make install

Now we can start an environment with python 3 and to install django and gunicorn:

mkvirtualenv myapp --python=python3 #make the environment using python 3
workon myapp #active the environment
python --version #must show Python 3.3.2
pip install django gunicorn

With django installed let’s start an project

django-admin.py startproject myapp

Inside the project directory, we need to create a file called runtime.txt containing the version of the python that we’ll use. This file will specify the version of python.

echo "python-3.3.2" > runtime.txt

Make a requirements.txt file to specify the dependencies.

pip freeze > requirements.txt

Make a Procfile to explicit what command should be executed to start the dyno.

echo 'web: gunicorn wsgi -b "0.0.0.0:$PORT"' > Procfile

We need to put our application on git

git init
git add .
git commit -m "initial commit"

Considering that you have heroku installed and you’re logged, we need to make a heroku repository

heroku create

let’s deploy our application

git push heroku master

See the application running on heroku with python 3 and django 1.5

heroku open