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

By Russell Barnes. Posted

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:

print("Hello World!")

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:

name = "Lucy"
print("Hello " + name)

>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.

n = 10

while n > 0:
  print(n)
  n-=1

print("Blast Off!")

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).

n = 3

def count(n):
  if n > 0:
    print(n)
    count(n-1)
  else:
    print("Blast off!")

count(n)

countdown.py

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.

class Animal():
  def __init__(self, c, n):
    self.creature = c
    self.name = n
    
  def get_creature(self ):
    return self.creature

  def get_name(self):
    return self.name
    
animals = []

animals.append(Animal("Dog", "Fido"))
animals.append(Animal("Cat", "Claws"))
animals.append(Animal("Mouse","Nibbles"))

for animal in animals:
  name = animal.get_name()
  creature = animal.get_creature()
  print(name + " is a " + creature)

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

From The MagPi store

Subscribe

Subscribe to the newsletter

Get every issue delivered directly to your inbox and keep up to date with the latest news, offers, events, and more.