Upgrade your Python version for easier debugging!
Published Nov 10 2022 10:03 AM 4,115 Views
Microsoft

Before joining Microsoft, I spent three semesters teaching Python in UC Berkeley’s CS61A, the first class in their CS sequence. I really liked Python as a teaching language and as a first language for many students, due to its relatively low usage of punctuation and its tendency to read like an English sentence.

However, as it is a text-based language, there are still many frustrating ways to mess up a Python program – spelling things wrong, indenting incorrectly, not using the right function for the job, etc. – and new programmers can have a particularly hard time understanding how to fix incorrect code. Teachers and TAs can help students decipher errors, but we’re not always around when they’re coding, especially in large classes (my class size ranged from 1000-2000!).


That’s why I’m very excited that the latest versions of Python have been steadily improving their error reporting and messages to be more specific and helpful.


Python 3.10 improvements

Many of the improvements began in Python 3.10 and were implemented largely by Pablo Galindo Salgado. For example, Python 3.10 has better messages for the oh-so-common mistake of mismatched parentheses and curly brackets:

 

 

 

File "example.py", line 1 
    expected = {9: 1, 18: 2, 19: 2, 27: 3, 28: 3, 29: 3, 36: 4, 37: 4, 
               ^ 
SyntaxError: '{' was never closed 

 

 

 

 

Python 3.10 added more information about indentation errors and their location:

 

 

 

def foo(): 
...   if lel: 
...   x = 2 
  File "", line 3 
    x = 2 
    ^ 
IndentationError: expected an indented block after 'if' statement in line 2 

 

 

 

 

And, my personal favorite: when Python 3.10 encounters undefined names (NameError), it uses a spell-checking algorithm to look for similar names that are defined.

 

 

 

schwarzschild_blackhole = 1916
>>> schwarschild_blackhole 
Traceback (most recent call last): 
  File "", line 1, in  
NameError: name 'schwarschild_blackhole' is not defined. Did you mean: schwarzschild_blackhole? 

 

 

 

 

I’ve seen so many students struggle with little spelling mistakes in their code, a single character wrong that is so hard for the human eye to spot but is fatal to program execution. Now Python can be the TA looking over their shoulder, helping them spot that mistyped character.

You can read more about the 3.10 enhancements in the release notes.


Python 3.11 improvements

Python 3.11 continued the trend of better error reporting, with more improvements from Pablo.

In 3.11 tracebacks, the interpreter points to the exact position in the line that caused an error, a particularly helpful feature for ambiguous messages.

 

 

 

 

Traceback (most recent call last): 
  File "distance.py", line 11, in  
    print(manhattan_distance(p1, p2)) 
          ^^^^^^^^^^^^^^^^^^^^^^^^^^ 
  File "distance.py", line 6, in manhattan_distance 
    return abs(point_1.x - point_2.x) + abs(point_1.y - point_2.y) 
                           ^^^^^^^^^ 
AttributeError: 'NoneType' object has no attribute 'x' 

 

 

 

Thanks to the little arrows below the line, it’s now clear that point_2 is the None-valued object, versus point_1. See more examples in the 3.11 release notes.


Upgrade your Python

Python 3.10 was first released Oct 2021, so it’s stable and supported most places. Python 3.11 was released less than a month ago, so it may still have a few minor bugs to work out, but it’s been working great for me in personal projects.

You can download either version from Python.org. You can also use them inside Github Codespaces, using this Python 3.10 or Python 3.11 template repo.

 

If you’re new to Python and this gets you excited about learning it (or teaching it!), check out our Python for beginners learning path.

If you have ideas for how Python could be even more helpful for new learners, post them in the Python Ideas forum. That’s where I posted an idea for improving the NameError response, and amazingly, Pablo implemented my request the week after! Thank you to the Python team for their commitment to making a highly usable and learnable language.

Co-Authors
Version history
Last update:
‎Nov 12 2022 09:40 AM
Updated by: