mercredi 13 mai 2015

Getting Started With Python

When I confess about my love for Python language, people often tell me they did not really get into it. They may be just polite… like thinking “using indentations to delimit code blocks?! Come on!”

or they may actually do want to learn! Here are my advices to start with this beautiful piece of technology.

Start with Python 3

As you may know, it still exists two branches of Python: Python 2 and Python 3. Today, Python 2 is maintained for compatibility issues as some specficic libs has not been upgraded yet. However, as a beginner, you should start with Python 3, as it comes with less gotchas (better support of unicode and one style of classes are two examples of improvements).

Install it with your favorite packet manager on linux, homebrew on OSX (choose python3 package) or download it from if you’re on Windows.

Choose a good editor

If you already are a software programmer, you may have a favorite editor so stick with it. If not, you may start with Idle, that is normally comes with Python distribution. I think the mandatory capacity for the editor in that context are:

  • Syntax highlighting to help you to learn Python’s syntax,
  • Auto-indentation because having to type them by yourself everytime is cumbersome.

Choose a good REPL

Without argument, python provides a REPL (Read Evaluate Process Loop). It’s a good idea but it’s not very convenient. You should install ipython. It will gives you:

  • Autocompletion with tab key for both python language and system path. It allows you to browse package content easily.
  • Easy access to documentation. Just type function then ? then return.
  • Lots of other cool thing that I let you dig by yourself.

In most of the time, I have a ipython running in a term to check stuffs while I’m coding.

You can install ipython with pip, the package manager that come with Python 3:

pip install ipython

On Windows, you'll need to add pyreadline package to get color and unix-like terminal key strokes.

pip install pyreadline

(you'll be prompted to do so when you start ipython without it anyway)

Ipython also come with a fonctionality called the notebook. It allows you to run a python interpreter from within a web app. It brings a lot of cool stuff, like editing markdown or displaying graphics and animations. To use the notebook, you'll have to install zero MQ wrapper for python and jsonschema.

pip install zmq jsonschema

Then run ipython3 with notebook option:

ipython3 notebook

Unit testing

To be confortable when learning a new language, I need a unit test framework. Python is delivered with unittest module, which is very much like the early versions of JUnit. I used it a lot. Then I tried py.test and I'll never look back!

py.test offers a very concise syntax to write tests and the runner come with a lot of options. Moreover, you can install additional plugin to do BDD or use it with a web framework. You can also install it with pip.

pip install pytest

Launch it with py.test command, and it will discover and run all the tests in a given directory structure.

Web framework

If you want to show what you code to your wife and your kids, you'd better display the result in a web browser than on an austere terminal window.

I advise you to start with bottle, which is a very simple yet efficient framework if you want to do simple things. Write functions to serve http routes, launch it and you're done.


I hope this article gave you the keys to enter the magical realm of Python programming language. I wish you'll have fun while learning it!