4 months ago

Thonny on a Raspberry Pi: using the new Python IDE in Raspbian

Use the new Thonny IDE in Raspbian on a Raspberry Pi to understand what’s going on in your code

Thonny is a new IDE (integrated development environment) bundled with the latest version of the Raspbian with PIXEL operating system. Using Thonny, it’s now much easier to learn to code. Thonny comes with Python 3.6 built in, so you don’t need to install anything.

Just open up the program, which you’ll find under Menu > Programming. It offers a lot of advanced features not currently available in the Python 3 (IDLE) program, which is still included with Raspbian.

See also: Programming a Raspberry Pi with Python

When you start Thonny, you’ll see a new script editor and a shell. As with Python 2/3 IDLE, you enter a program in the script editor and run it in the shell. You can then use the shell to interact directly with the program; accessing variables, objects, and other program features.

Thonny has a range of additional features that are perfect for learning programming. One of the best features is a powerful, but easy-to-use, debug mode. Instead of running your program, it steps through the code line by line. You can see the variables and objects being created, and values being passed into functions or assessed by comparators.

You often find debuggers in powerful IDEs, but they tend to require you to manually set breakpoints (places where the program freezes so you can examine the code). The approach in Thonny is much more straightforward. It also has a range of panels that enable you to inspect various items, such as variables, objects, and the heap (the memory space where items are stored).

There’s some pretty good stuff in Thonny for young coders. The ability to step through your programs makes it much easier to understand what happens when you hit Run.

>STEP-01: How to use Thonny

Thonny

Click the Raspberry Pi Menu icon in the top left of the screen and choose Programming > Thonny Python IDE. We’ve used File > Increase Font Size so you can see the text more clearly. Enter this line of code in the script editor:

Now choose File > Save and name the program hello.py. Click Run current script (or press F5) to see the output in the shell. As with IDLE, you can also enter commands directly in the shell, such as:

>STEP-02: Coding in Thonny

Thonny

Let’s see how you can walk through a file and see a variable change. Create a new script (File > New) and enter the code in countdown.py.

Click Run and the code will display ‘10, 9 …. 2, 1, Blast Off!’ The n variable starts at 10. A while loop prints it, and decreases its value as long as it remains above zero.

>STEP-03 Debug Python code in Thonny

Thonny

Choose View > Variables and a new window appears displaying n and its current value (which is zero). Now let’s run through it one step at a time. Click ‘Debug current script’. The first line will be highlighted. Click Step Into and the value will be highlighted. Click it again, and both ‘n’ and ‘10’ are placed in the Variables window.

>STEP-04: Step through Python code in Thonny

Thonny

Keep clicking Step Into and you will see the value of variable n (which is 10) added to the comparator and evaluated to True. Then the while loop will activate, the value of n will be displayed to the console, and n will decrease by 1. Click Step Out to run through the while loop and back to the main code.

>STEP-05: Understand Recursion in Thonny

Thonny

Thonny’s debug mode makes it easier to understand concepts such as recursion. Our countdown_recursion.py program runs a countdown recursively (a function which calls itself from inside itself).

When the function calls itself, a new window appears with the function. Keep stepping through to see the values updated.

>STEP-06: Look at Heap and objects in Thonny

Thonny

For a more detailed view, enter View > Heap and View > Objects. Now, as you work with object-oriented code, you can select objects in the Heap or Variables window and use the Object Inspector to check their type and attributes. The animals.py code creates animal objects with creature and name instance variables.

Win! One of 10 Raspberry Pi 3 & Official Case signed by Eben Upton. Click here to enter

  • Ben Simmons

    Many thanks for putting in the hard work looking for something like this. It is exactly what I have been looking for, but not had the time or have come across it. I found IDLE lacking as I have been spoilt by even MS Offices built VBA tools similar to this.

    It make life so much easier being able to see what’s going on and spot silly errors.

    I’ve been using it for the past to days and really like it.

    One question, how do we update pip to the version this needs to be properly integrated? The default Raspian pip/pip3 is far too old.

  • Andrew Oakley

    Anyone know whether this performs okay on a B+ ? At Cotswold Jam we usually run two concurrent workshops; one room with B+s, and one room with 2/3s. Any workshop requiring a browser gets the 2/3s.

  • J.Teigland

    had to install Thonny manually on my Rpi2B from Add/Remove Software BUT seems to work fine (latest Jessie Upgrade w/Pixel)

  • Jim Callahan

    The first screenshot of Thonny looks good; is this the default, out of the box configuration?
    The arrangement of windows reminded me of RStudio (used for the R language which I love). Also could you add a window for graphics output and make it even more like RStudio. 😉