Starting with Django on Windows 10

29 februari 2016 - Joeri - Development - Django

Recently I upgraded to Windows 10, using a clean install and one of the first things to install was Python. Although I recommend developing with Python on Linux, I've been doing it on Windows for as long as I can remember.

Windows 10, Python 2.7.11 and Python 3.5.1 had some nice updates to get up and running much quicker than before. I install both because some projects still require Python 2.7 but for new projects you should definitely install Python 3.5.

For the impatient

  1. Download and install Python 3, PostgreSQL 9, PostgreSQL bindings, Git and PuTTY
  2. Add the following environment variables (make sure to use the program path's where you installed the above tools):
    GIT_SSH=C:\Program Files (x86)\PuTTY\plink.exe
    PATH=C:\Program Files (x86)\Python35-32;C:\Program Files (x86)\Python35\Scripts\;C:\Program Files\Git\bin;C:\Program Files\PostgreSQL\bin
    
  3. In an Aministrator command prompt:
  4. > python --version
    Python 3.5.1
    > python -m pip install --upgrade pip
    > pip install virtualenv ipython pyreadline
    
  5. In a regular command prompt:
  6. > virtualenv env
    > env\scripts\activate
    (env)> pip install Django
    (env)> django-admin startproject myfirstproject
    (env)> python manage.py runserver
    

Have fun!

Step by step

Here goes.

NOTE: Although I'm using a 64-bit OS, I only install the 32-bit versions of Python. In my experience, this makes compiling libraries a lot easier. Also, most pre-compiled libraries are only available in 32-bit versions. All other programs can be 64-bit without problems.

Download all the things

Below a list of what to download, install and why.

Python

Python 2.7.11 and Python 3.5.1

You need to get started right?! Only one version is required, two is recommended.

During the setup, make sure to check the box to add Python to your path. Also, you can change the installation path to point to your C:\Program Files (x86)\ if you want, just make sure to use the DOS-notation: C:\Progra~2\. If you happen to install the 64-bit versions, use C:\Progra~1.

Database

PostgreSQL 9.5.1

Recommended database engine. You can pick Windows x86-64. The pgAdmin III tool that comes with this download is far from perfect but provides a handy interface.

http://www.stickpeople.com/projects/python/win-psycopg/ MariaDB 10.1.12 (MySQL compatible)

Alternatively, or additionally, you can install MySQL or its open source counterpart, MariaDB. It comes bundled with HeidiSQL which works quite nice.

Git

Git 2.7.2

The nice thing about this download is that it also includes alot of nice Linux-like shell commands. For this to work, you need to select X during the installation wizard.

git config --global user.name "Your Real Name" git config --global user.email "you@email.com" GIT_SSH=C:\Program Files (x86)\PuTTY\plink.exe Call plink to bitbucket.org / github.com to auth and save key

PuTTY

PuTTY beta 0.66

https://docs.djangoproject.com/es/1.9/topics/i18n/translation/#gettext-on-windows https://docs.python.org/2/using/windows.html Visual Studio 2008: https://go.microsoft.com/?linkid=7729279 https://go.microsoft.com/fwlink/?LinkId=104679 (iso) https://docs.python.org/3/using/windows.html Python 3.5 was compiled with Visual Studio 2015 https://www.microsoft.com/en-us/download/details.aspx?id=48146 NodeJS http://rubyinstaller.org/downloads/ gem install sass

Before we begin

You can install some libraries globally, to prevent installing them over and over again. Start cmd as Administrator (Press Windows-key, type cmd, right click it and select Execute as Administrator).

Check which version is first in your PATH:

> python --version
Python 2.7.11

If you want to have a different default version, you just need to change the order of your PATH. You can do so in System > Advanced system settings, and go to the Advanced tab and click Environment variables. Make sure your prefered version is at the top (both the regular Python path, and the Python scripts path).

Let's upgrade pip first. This also shows how to differentiate between the different Python versions that are globally installed. Python does not install an alias with its version number, unlike most scripts. You can make an alias yourself but this is one of the few times you probably need to differentiate between the two versions. like this, since we're installating virtualenv next.

> python -m pip install --upgrade pip
> C:\Progra~2\Python35\python -m pip install --upgrade pip

With virtualenv every project has its own "virtual environment", with a specific version of Python available, and only the libraries required for the project you are working on. No need to have Administrator privileges anymore to install libraries.

> pip2 install virtualenv
> pip3 install virtualenv

And then some convenience libraries. For readability, I only install them now for Python 3.

> pip3 install ipython pyreadline

Let's close our cmd with Administrator privileges and open a new one without those privileges.

Your first project

Let's create a virtual environment for Python 3 and install Django! I pass in the option --system-site-packages to include access to the globally installed libraries we installed earlier.

> virtualenv-3.5 --system-site-packages env
> env\scripts\activate
(env)> python --version
Python 3.5.1

Note how after "activating" the virtual environment, you don't need to differentiate between Python versions anymore. To deactivate a virtual environment, just type "deactivate".

(env)> pip install Django
(env)> django-admin startproject myfirstproject
(env)> python manage.py runserver
Performing system checks...

System check identified no issues (0 silenced).

You have unapplied migrations; your app may not work properly until they are applied.
Run 'python manage.py migrate' to apply them.
February 29, 2016 - 12:23:58
Django version 1.9.2, using settings 'myfirstproject.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.



Latest Tweets