Welcome to the inevitable! No matter how experienced you become, you will write code that crashes, as I’m sure you’ve experienced plenty of times in the core program. In fact, as a Data Engineer, you will often deal with messy data that breaks your pipelines.
Learning to read errors and fix them (debugging) is a superpower. In this chapter, we will learn how Python tells us something went wrong and how to investigate it.
Just like in JavaScript, errors in Python generally fall into three categories:
These happen when Python doesn’t understand your code because you broke the rules of the language. Python catches these before it runs your program.
# ❌ SyntaxError: expected ':'
if True
print("This won't run")
Common Syntax Errors:
: at the end of if, for, def.( or brackets [.Since you have the VSCode Python Extension installed, your IDE will flag these when you write the code by using Pylance, VSCode’s default Python language support tool.
These happen while the program is running. The syntax is correct, but something illegal happened during execution.
# ❌ ZeroDivisionError: division by zero
result = 10 / 0
# ❌ NameError: name 'x' is not defined
print(x)
The program runs without crashing, but it does the wrong thing. These are the hardest to catch because Python won’t give you an error message.
# 🐛 Logical Error: Calculating average incorrectly
numbers = [10, 20, 30]
average = sum(numbers) / 2 # Should be divided by len(numbers), which is 3!