Thonny



Thonny

  1. Thonny Download For Pc
  2. Thonny Download
  3. Thonny Micropython

Thonny comes with Python 3.7 built in, so just one simple installer is needed and you're ready to learn programming. (You can also use a separate Python installation, if necessary.) The initial user interface is stripped of all features that may distract beginners. Thonny is a beginner-friendly Python IDE, developed in University of Tartu, Estonia, which takes a different approach as its debugger is designed specifically for learning and teaching programming. Although Thonny is suitable for even total beginners, this post is meant for readers who have at least some experience with Python or another.

Learning to program is hard. Even when you finally get your colons and parentheses right, there is still a big chance that the program doesn’t do what you intended. Commonly, this means you overlooked something or misunderstood a language construct, and you need to locate the place in the code where your expectations and reality diverge.

Programmers usually tackle this situation with a tool called a debugger, which allows running their program step-by-step. Unfortunately, most debuggers are optimized for professional usage and assume the user already knows the semantics of language constructs (e.g. function call) very well.

Thonny is a beginner-friendly Python IDE, developed in University of Tartu, Estonia, which takes a different approach as its debugger is designed specifically for learning and teaching programming.

Thonny ide

Although Thonny is suitable for even total beginners, this post is meant for readers who have at least some experience with Python or another imperative language.

Thonny is included in Fedora repositories since version 27. Install it with sudo dnf install thonny or with a graphical tool of your choice (such as Software).

When first launching Thonny, it does some preparations and then presents an empty editor and the Python shell. Copy following program text into the editor and save it into a file (Ctrl+S).

Let’s first run the program in one go. For this press F5 on the keyboard. You should see a triangle made of periods appear in the shell pane.

Did Python just analyze your code and understand that you wanted to print a triangle? Let’s find out!

Start by selecting “Variables” from the “View” menu. This opens a table which will show us how Python manages program’s variables. Now run the program in debug mode by pressing Ctrl+F5 (or Ctrl+Shift+F5 in XFCE). In this mode Thonny makes Python pause before each step it takes. You should see the first line of the program getting surrounded with a box. We’ll call this the focus and it indicates the part of the code Python is going to execute next.

Thonny debugger focus

The piece of code you see in the focus box is called assignment statement. For this kind of statement, Python is supposed to evaluate the expression on the right and store the value under the name shown on the left. Press F7 to take the next step. You will see that Python focused on the right part of the statement. In this case the expression is really simple, but for generality Thonny presents the expression evaluation box, which allows turning expressions into values. Press F7 again to turn the literal 1 into value 1. Now Python is ready to do the actual assignment — press F7 again and you should see the variable n with value 1 appear in the variables table.

Continue pressing F7 and observe how Python moves forward with really small steps. Does it look like something which understands the purpose of your code or more like a dumb machine following simple rules?

Function call is a programming concept which often causes great deal of confusion to beginners. On the surface there is nothing complicated — you give name to a code and refer to it (call it) somewhere else in the code. Traditional debuggers show us that when you step into the call, the focus jumps into the function definition (and later magically back to the original location). Is it the whole story? Do we need to care?

Turns out the “jump model” is sufficient only with the simplest functions. Understanding parameter passing, local variables, returning and recursion all benefit from the notion of stack frame. Luckily, Thonny can explain this concept intuitively without sweeping important details under the carpet.

Copy following recursive program into Thonny and run it in debug mode (Ctrl+F5 or Ctrl+Shift+F5).

Press F7 repeatedly until you see the expression factorial(4) in the focus box. When you take the next step, you see that Thonny opens a new window containing function code, another variables table and another focus box (move the window to see that the old focus box is still there).

Thonny stepping through a recursive function

This window represents a stack frame, the working area for resolving a function call. Several such windows on top of each other is called the call stack. Notice the relationship between argument 4 on the call site and entry n in the local variables table. Continue stepping with F7 and observe how new windows get created on each call and destroyed when the function code completes and how the call site gets replaced by the return value.

Now let’s make an experiment inside the Python shell. Start by typing in the statements shown in the screenshot below:

As you see, we appended to list b, but list a also got updated. You may know why this happened, but what’s the best way to explain it to a beginner?

When teaching lists to my students I tell them that I have been lying about Python memory model. It is actually not as simple as the variables table suggests. I tell them to restart the interpreter (the red button on the toolbar), select “Heap” from the “View” menu and make the same experiment again. If you do this, then you see that variables table doesn’t contain the values anymore — they actually live in another table called “Heap”. The role of the variables table is actually to map the variable names to addresses (or ID-s) which refer to the rows in the heap table. As assignment changes only the variables table, the statement b = a only copied the reference to the list, not the list itself. This explained why we see the change via both variables.

Thonny in heap mode

Thonny Download For Pc

(Why do I postpone telling the truth about the memory model until the topic of lists? Does Python store lists differently compared to floats or strings? Go ahead and use Thonny’s heap mode to find this out! Tell me in the comments what do you think!)

If you want to understand the references system deeper, copy following program to Thonny and small-step (F7) through it with the heap table open.

Even if the “heap mode” shows us authentic picture, it is rather inconvenient to use. For this reason, I recommend you now switch back to normal mode (unselect “Heap” in the View menu) but remember that the real model includes variables, references and values.

The features I touched in this post were the main reason for creating Thonny. It’s easy to form misconceptions about both function calls and references but traditional debuggers don’t really help in reducing the confusion.

Besides these distinguishing features, Thonny offers several other beginner friendly tools. Please look around at Thonny’s homepage to learn more!

When you are developing a Python 3 application with Thonny, you may want to get Thonny to use a virtual environment to run and debug your application.

With this in mind, this post shows you how to associate a Python 3 virtual environment with Thonny.

Creating a virtual environment with python3-venv

This tutorial was created with Thonny running on Raspbian Stretch. The steps to associate a Python 3 virtual environment with Thonny running on Windows should be similar.

Before proceeding further, make sure that you had created a virtual environment. In case you need it, you can follow one of the following tutorials to create your Python 3 virtual environment:

  • Create a virtual environment with python3-venv on Linux or Unix.

For the purpose of this post, let's assume that we had created a Python 3 virtual environment at /home/pi/my-venv.

Steps to associate a Python 3 virtual environment with Thonny

Firstly, open up your Thonny IDE.

If you see that your Thonny IDE does not have the menu bar, click on Switch to regular mode on the top right corner. After you had done so, Thonny will show you a confirmation message:

Click on Ok and reopen Thonny. After Thonny had been reopened, it will show a menu bar at the top:

Thonny Download

Given that, go to Tools ->Options:

Click on the Interpreter tab:

Thonny Micropython

Mode

As shown above, Thonny is using the same Python interpreter which runs Thonny to run your Python 3 application. Click on the combo box and choose Alternative Python 3 interpreter or virtual environment:

Click on Locate another python executable... and a file dialog will appear for you to choose the python executable inside your virtual environment:

Click Ok to confirm your selection:

Click Ok again to confirm your configuration changes. Once you had done so, you will see that Thonny restarts the shell below with the Python executable from your virtual environment:

Given that, Thonny will run your Python 3 application inside your virtual environment.

About Clivant

Clivant a.k.a Chai Heng enjoys composing software and building systems to serve people. He owns techcoil.com and hopes that whatever he had written and built so far had benefited people. All views expressed belongs to him and are not representative of the company that he works/worked for.

← Previous post
How to create a virtual environment for your Python 3 application with python3-venv in Linux or Unix
Next post →
How to setup Raspbian Stretch on Raspberry Pi 3 for developing Python 3 applications