The Python development tools
Now that the Pi is set up and running Raspbian, we can have a look at some of the tools we will use to develop Python scripts (small text files containing commands) and applications. Most of the time we will be using either the interactive Python terminal to execute the code line by line or the python
executable to run full scripts and applications.
We will first look at the interactive terminal. First open a terminal by clicking on the black monitor icon in the top right corner of the desktop. This will open an LXTerminal window. In this window, type python
and press Enter. This will start the interactive terminal as shown in the following screenshot:
From here we can type the Python code line by line; each line is executed as soon as it is typed, making this tool useful for quick testing and debugging (I also find that it makes a nice command line calculator). To demonstrate this, type in the following code and press Enter:
print "Hello, world!"
This will print the test Hello, world!
on the line next to where you typed it in, as shown in the following screenshot:
The python
executable can also be used to run the existing Python script files (which have the .py
file extension), which we will look at later in the chapter.
One alternative to the interactive terminal is the IDLE Integrated Development Environment (IDE) which can be used both as an interactive terminal and a source file editor, and provides syntax highlighting for the Python files. It can be found by selecting Python 2 from the Programming submenu of the main menu on Raspbian, as shown in the following screenshot:
When first opened, it will be in the interactive terminal mode and can be used in the same way as the terminal ran from the command line, as shown in the following screenshot:
Python 2 versus Python 3
You will notice that there are two versions of Python installed by default on Raspbian: Python 2.7 and Python 3.1. Whilst the fundamentals of Python programming have not greatly changed between the two versions, there are notable differences that may prevent a code that was written for one version from working when executed with the interpreter for a different version.
For this reason, we will only use Python 2.7 in this book as this has the widest library support and is still the default Python version on many operating systems.
Note
More information of the differences between Python versions is available on the Python Wiki at wiki.python.org/moin/Python2orPython3