Tuesday, 9 February 2016

Baby steps with Python and Django

I've neglected this blog lately, but I've decided to pick it up again.  I've started a new project for my self in Python and I want to keep a bit of a track of what I've learned.

I've basically been following the (rather excellent) Django tutorials so far, but today I wanted to start deviating.  Here's how I've gotten to where I am with the project so far:

Used Homebrew to install Python 3 (my Mac comes with Python 2.7 installed by default, but I wanted to go with the latest version):


$ brew install python3

Then it was time to set up a Virtual Environment for my project.  According to this Ask Ubuntu post, virtualenv isn't set up to run with Python3 yet, so I used pip3 rather than the normal pip.

$ sudo pip3 install virtualenv
...
$ virtualenv-3.3 project

I'm using PyCharm to manage my project, which comes with a built in Terminal window and Python Console.  Within the Terminal window, we need to use the virtualenv for the project:

$ source bin/activate

Then it's a case of installing Django

$ pip install django

Installing the MySQL connector

$ pip3 install mysqlclient

Oops - but that didn't work:  OSError: mysql_config not found.  Turns out the mysqlclient connector for Python needs MySQL (or at least the developer library) installed.  Despite it being about running on Linux, I found this Stack Overflow post quite useful.  Back to Homebrew to fetch MySQL:

$ brew install mysql

After that mysqlclient installed without a project.  Finally it's time to create a project:

$ django-admin startproject project

And there you go - all of the building blocks are in place to start things off!

No comments:

Post a Comment