Other Features in Thonny
Thonny has a bunch of other great features to help you write more accurate code more quickly. Thonny highlights lines with syntax errors so you can find them more easily. You can also View the Assistant to see what the problem is more detail.
Thonny can highlight the names of variables. Because it knows about variable scope, it can tell the difference between local and global variables. This feature isn’t turned on by default, but you can opt into it.
Thonny can also perform code completion. This is handy because it can let you know what’s available from a module that you’ve imported. It can also help you avoid typos and reduce the amount of time it takes to type.
Thonny’s package manager is also useful. Packages are one of Python’s greatest strengths. They are downloadable modules that allow you to extend Python’s functionality and create programs without having to reinvent the wheel.
00:00 Thonny’s Other Great Features.
00:05 Thonny has a number of other great features which will help you write more accurate code more quickly, the first of which is finding syntax errors.
00:19 Thonny can find a syntax error in your code and highlight it to you. This is done as you’re creating the code. So as you’re typing the code, it’s checking for syntax errors and it will highlight them, in this case, by leaving a highlighted line. So, here, in a print statement, by leaving out the last of two brackets, we can see that that line is highlighted.
00:44 If I move to another line and try typing, you can see I’ve got an indent, which seems a little bit odd, and that should probably highlight to you that something’s wrong.
00:53 If I start another print statement, it doesn’t help. It still looks wrong because Python knows that this is wrong and we’re looking for another bracket. As soon as we put that in place, the highlighting disappears because we’ve got good syntax.
01:09 Thonny will always be looking over your shoulder to make sure your code has good syntax, although you will notice as you create the code, it’s initially highlighted as being incorrect because we haven’t got a closing bracket. And while you can put that in as you add an extra code inside there, such as a function call, until you put in the correct closing bracket it will be highlighted as wrong.
01:33 Until you’ve finished typing a line, probably best to ignore it. But if you think you’ve finished typing a line and it’s still highlighted, then there’s definitely something you need to look at.
01:43 You can see what’s really going on with this by going to View > Assistant. Here we can see that it’s blank at the moment because the Assistant view doesn’t work until you try to run the program.
01:58
So because the program has been saved previously, we can click Run, which will save it and then run it. And as we can see, running it has given us a SyntaxError
. There’s that SyntaxError
in the shell.
02:11
And it’s also giving us that same SyntaxError
in the Assistant. So you can see here, we have the same error message as Python has given us—the same as that one there—but it does give us some extra, so Unbalanced parentheses, brackets or braces, so it says that ‘(‘ at line 8 is not closed by the end of the program.
02:34
That’s helpful. We can close that. Then we can run the program and we can get the predictably large output from factorial(100)
.
02:48 Variable Highlighting. Thonny can highlight the names of variables, and because it knows about variable scope, it knows the difference between a local and a global variable.
02:59 This isn’t turned on by default, so on a Mac, you would need to go up to the Thonny menu and then Preferences, and then go to the Editor tab and tick Highlight matching names.
03:12 In Windows, it’s under Tools > Options, but then Editor and Highlight matching names is the same. On Ubuntu, it’s the same as Windows, so Tools > Options, then you go to the Editor tab and click Highlight matching names.
03:29
Back on the Mac, we’ve ticked Highlight matching names and hit OK. And now highlighting a name such as my_calculator
by double-clicking it shows that this is only in one place in that scope.
03:44 It doesn’t highlight that one there, but if I highlight that one, you can see that it doesn’t highlight the one inside the function. Now, occasionally it can get maybe a little bit confused because highlighting the one on line 9 highlights all of them, but if you highlight where it’s actually defined, it will generally do a good job of ensuring they’re separate from each other, as you can see here.
04:06 Something I find useful, also in Preferences, is highlighting local variables. That means that local variables are highlighted in italics, which I’ve found useful and helps remind me that these are local variables and not global variables. And if I’ve been maybe a little foolish with my naming, this helps point out the fact that this is a different variable, even though it may share the same name as something in the global scope.
04:44 Thonny can perform what’s called code completion, filling in the code which is the rest of your line. This can be doubly useful, as you’ll see here. There’s two different areas where it can work.
04:55
The first one is it can tell you what’s available from a module that you’ve imported. Here is the datetime
module which has been imported. And now if I type datetime.
with a dot (.
) and hit Tab, I can see all of the methods which are available inside that module.
05:11 I can scroll through them using the cursor keys and select one with Enter.
05:17
And if I want to see what’s inside that module, I can again type .
then hit Tab, and here I can see some more. And inside this case we have .now()
, which can be finished off with a bracket.
05:34 So that’s one way that Thonny can help you—by showing you the available methods. But there’s another way, which can be really helpful in terms of typos, reducing the amount of time you need to type things, but also making sure you don’t make mistakes. Here we defined two variables.
05:49
One is my_name = 'Darren'
. Secondly, my_birthday = 'yesterday'
. So if we type my_
and then hit Tab, we have only two options, my_birthday
or my_name
, which we can select as before.
06:07
But if we hit my_b
and then hit Tab, because there’s only one option, it will fill that in for us, which can save time but also mean you’re much less likely to make an error—providing, of course, you’ve picked the right variable in the first place.
06:24 Many editors do include this, but it’s nice that Thonny does this. It’s a fairly standard thing that the Tab key does this completion for you, and once you get used to it, you probably won’t look back.
06:36 Thonny: the Package Manager. If you’re not aware, packages are one of Python’s greatest strengths. They’re downloadable modules which allow you to extend Python’s functionality and create programs without having to reinvent the wheel. Very often, a function that you want to implement will already have been created by somebody and will be available as a free module that you can download to supercharge your Python programming. A package manager allows you to open these packages quickly and easily.
07:07 Thonny’s package manager is found under the Tools menu. So clicking on Tools > Manage packages will take you there, and here you can see it on the left-hand side.
07:19
There are all the currently installed packages, but in addition, you can search for a package from the Python Package Index. Here, simplecalculator
has been chosen, and then you can choose to install that after checking out the information and making sure that it is the package that you want, possibly using the links.
07:38 Clicking Install will download and install that, and we can see it’s been downloaded and now it’s installed and it’s added to the list of packages on the left.
07:50 Now that that’s been installed, we can import it in the normal way.
Become a Member to join the conversation.