Python error messages(Tracebacks) can be daunting as a python beginner. Sometimes you will end up with a page of error message not knowing where to start and where to end and, most importantly, fixing it is another nightmare. Before you begin to doubt your skills and feel like you aren’t equipped enough. Remember, Python or any other programming language is designed so that people can use it. A team of developers works on Python source code to make it better every day. With a few steps, you can understand error messages without wanting to break your head.
Reading errors doesn’t have to be daunting. You don’t have to go back to the tutorial hell and not progressing your learning journey. It takes a little bit of practice and some consious reading between the lines. Use the following strategy as long as reading error messages feels normal.
Let’s look at a few tracebacks today and see how we can spot the error messages. Whenever you see a traceback error message, break it down into chunks using the following framework.
Traceback (most recent call last): File "process.py", line 6, in <module> c.is_industrial() File "/Users/bhavaniravi/bhava/projects/python-advanced/tracebacks_101/vehicle.py", line 12, in is_industrial return self._is_industrial AttributeError: 'Car' object has no attribute '_is_industrial
No I am not going to show the code here because, the goal of this exercise is to extract as much info as posssible from the traceback.
What is the error message?
Look at the last line of the traceback, The ultimate answer to what broke your code lies there ___ suspenseful music. Most times, it comes with enough explanation to fix the error. In my case, I have an
AttributeError: 'Car' object has no attribute '_is_industrial`
Can you make sense of this error just by looking at it? It’s okay if you don’t let’s go to the next step.
Where does it happen?
The goal of asking this question is to find the exact line number of the error. This can be tricky as a newbie, and trickier are using external packages. Python tracebacks don’t differentiate your code and the packages you use. So in this step, the goal is to read through the traceback and find the file you wrote.
File "process.py", line 6, in <module> c.is_industrial() File "/Users/bhavaniravi/bhava/projects/python-advanced/tracebacks_101/vehicle.py", line 12, in is_industrial return self._is_industrial
Make a note of the line numbers and the file names.
Why does it happen & How to fix it?
Now comes the most obvious question yet the most difficult to answer. There is a wide range of possibilities for something to go wrong. Time to put on your detective hat and combine what we know so far
# Q1 AttributeError: 'Car' object has no attribute '_is_industrial` # Q2 File "/Users/bhavaniravi/bhava/projects/python-advanced/tracebacks_101/vehicle.py", line 12, in is_industrial return self._is_industrial
Now write it in plain English. We know we have an attribute error. So there is a Class in my code called
Car which does not have
_is_industrial, but in
line 12 of
vehicle.py the variable is referenced
The fix to this may vary depending on the use case and design of the code. Next time when you come across a daunting error message, pause and ask
- What is the error message?
- Where does it happen?
- Why does it happen?
Once you do it enough times, it will build into your working memory, and you don’t have to go through this exercise anymore. Now you know the only way to get better is by making mistakes, Quite Literally.