Six Pearls Designs

Developing Django on Windows

2013 March 28

As a developer and scientist, I prefer to use Linux. As an engineer I am sometimes forced to use Windows. Well I recently decided to figure out how to setup Windows (Win7 in this case) for Django development. It turns out, it actually isn't too hard.

pip and fabric on Windows

Some critical development tools are pip, virtualenv, and fabric. If you install in the following order, you can avoid headaches:

  1. Install Python. I use Enthought's EPD free Anaconda for access to scipy, numpy, and iPython, but the normal installer from python.org would work too
  2. Download and unzip pip. Install by going into the expanded directory and running python setup.py in a command prompt.
  3. Download and install PyCrypto for windows

If you want a raw-ish Windows experience, you can now use pip to install fabric, virtualenv, and virtualenvwrapper-win. pip install fabric virtualenv virtualenvwrapper-win from a command prompt. I like to use Cygwin because I'm more comfortable at a *nix prompt, instructions are below.

At this point you have virtualenv and virtualenvwrapper, which are standard tools for modern web development. I've noticed that things behave better if I don't create my virtualenvs using mkvirtualenv from the virtualenvwrapper package. Instead, go to C:\User\<username>\Env (or other %VIRTUALENV_HOME% location) and use virtualenv to start a new environment. Doing this allows virtualenvwrapper-win's workon command to work.

virtualenv and virtualenvwrapper with cygwin

If you want to use Cygwin, follow the above steps 1-3, then:

  1. Set the %PYTHONHOME% system variable to the python base directory, (i.e. C:\Python27\) and adding the python base directory and script directory (i.e. C:\Python27\Scripts) to your %PATH% system variable.
  2. Install Cygwin WITHOUT Python. The previous step tells Cygwin to use the Windows binary.
  3. Install Cygwin-Virtualenvwrapper using pip install https://bitbucket.org/cliffxuan/virtualenvwrapper-for-cygwin-windows-python/get/tip.tar.gz
  4. Install virtualenvwrapper-win using pip install virtualenvwrapper-win
  5. Make a symlink between Cygwin's virtualenvhome directory and Windows's using ln -s /cygdrive/c/Users/<USER>/Envs/ ~/.virtualenvs
  6. Add the following to Cygwin's .bashrc file:

    export VIRTUALENVWRAPPER_PYTHON="/cygdrive/c/Python27/python.exe"
    export VIRTUALENVWRAPPER_VIRTUALENV="/cygdrive/c/Python27/Scripts/virtualenv.exe"
    source $HOME/cygwin_virtualenv/virtualenvwrapper.sh
    

    Adjust the variables to point at the appropriate executables.

If you installed in order, cygwin and Windows should play nicely with their shared virtualenvs. Be sure to heed my advice above about creating virtualenvs though. You can now install other handy libraries, like fabric.

Warning: by default, the Cygwin python command does not initiate the normal interactive shell. Use python -i to get python to run as expected.

Django and Python on Windows

Whether or not you set up Cygwin, you now have two options for installing Python libraries: (1) from Windows installers (officially from the package website or unofficially from Christoph Gohlke) and (2) from pip. For bigger libraries like PIL, I prefer to use the windows installer into my global Python site-packages directory. For smaller libraries like Django apps, I use pip from within my virtualenv. Just take care when installing windows packages globally; if the library gets gunked up, the easiest solution is often to uninstall Python completely and start over.

In any case, now you just need a good text editor and version control system and you can be a full fledged django and/or python developer on Windows, with a variety command prompt options to test your prototypes and keep your environments isolated.

Update for Msys / MsysGIT (8 May 2013)

My old (6 years is a pretty long life by modern technology standards!) laptop bit the dust, and I ended up getting a new laptop and trying to use the official Windows version of git (msysgit) in the new installation. You can follow the above instructions to get through getting virtualenv and virtualenvwrapper-win installed. Then...

  1. Install msysgit. I recommend installing it in a path without any spaces-- I had issues getting through Step 4 (below) using the default path.
  2. Install virtualenvwrapper using pip
  3. Copy the following files from the msys extnernal binary packages's msys\bin folder to your git installation's bin folder:
    • mktemp.exe
    • fmt.exe
    • msys-intl-8.dll
    • msys-iconv-2.dll
  4. Make the following changes to your git's .bashrc file:

    export MSYS_HOME=</path/to/Git>
    export WORKON_HOME=$HOME/Envs
    source </path/to/Python/>/Scripts/virtualenvwrapper.sh
    

It works surprisingly quickly without much instability, and gives you the option to run all development commands from either the windows command prompt or the msys prompt.