The world’s leading publication for data science, AI, and ML professionals.

How to Crack Python Errors Like a Pro

As you crack manually an exception, the developer you become is more important than what you fix.

Photo by Chris Ried on Unsplash
Photo by Chris Ried on Unsplash

Python is a high-level programming language that is closer to natural human language. The benefit of this is that Python code is easy to comprehend and simple to implement. 48% of developers work with it, according to the 2021 Stack Overflow survey. In the coming years, this ratio will surely continue to grow.

Indeed, Python has initially attracted academics and professionals who use coding as a way to prototype their ideas, to demonstrate concepts, and to implement proof-of-concepts. Nowadays, Python has already conquered many fields of software applications including Devops scripting, Machine Learning ( NumPy, pandas, sklearn, and Tensorflow and Web backends (Django and Flask).


In this post, I introduce a practical strategy to crack Python errors for beginners, starting by showing the essential strategy steps for this well-known exception, ZeroDivision, raised in the below code example.

Python Error Cracking Steps on an Illustrative Example
Python Error Cracking Steps on an Illustrative Example

Our proposed error cracking strategy consists of three steps: (1) Scroll until you reach the bottom to find the error type; (2) Examine carefully the inline message that appears next to the error type in order to understand how it occurred; (3) Look closely at the trace of function calls and follow the horizontal arrows until you identify which line of code is faulty.

The ZeroDivisionError is a prevalent exception that points out directly to the root cause. We can consider them as specific exceptions such as FileNotFoundError and UnicodeError. Even beginners can find the code recipes on StackOverflow(SO) for fixing them. Nevertheless, there are generic exceptions, mostly the result of typos and careless mistakes during a deadline rush. Especially when a lot of the details in the error message pertain to your application code, googling these errors can be misleading due to the wide variability of possible causes.


The remaining paragraphs will provide you with an overview and explanation of some common types of errors. The above-described cracking method can better solve these types of errors. In this way, you’ll be able to save a great deal of time and effort spent reading posts about other SO users’ errors. Besides, you will also become more proficient at Debugging yourself through this process, which will teach you more about the Python language.

SyntaxError

Raised when the parser encounters a syntax error. – Python Official Docs

A Python parser cannot recognize the structure or sequence of tokens in a running program, which leads to syntax errors. Most often, this is due to missing or unnecessary characters, including commas, parentheses, etc.

In this example of SyntaxError, the occurred errors’ type and message are clear indicators that the code statement is syntactically invalid. Nevertheless, the difficulty of fault localization differs between them.

  • First Cell: We look at the traceback to determine the cause (Here, there is just a single line of code 1). A small vertical arrow points out the exact location of the syntax error on this faulty line. So, you’ve spotted it. Just get rid of the comma in-between the for loops.
  • Second Cell: It turns out that the code line (11) is not the faulty one, and the SyntaxErroris due to the missing parentheses in line (9). The parser, however, incorrectly identifies this location since it expects a closing parenthese there (rather than a new code statement), but it didn’t find it.

Therefore, we should analyze reversely the code by starting at the indicated location of the SyntaxError. By not following this bottom-up debugging technique, you may find yourself searching StackOverflow for syntax errors in contexts that have similar keywords, names, and libraries to your own.

NameError

Raised when a local or global name is not found. – Python Official Docs

Basically, developers get this error when they try to use a non-existent variable or function.

This is often caused by a misspelling in the name, such as the above example, where we spelled incorrectly the name of the function from the previous code snippet. This can happen when the developer forgets to import or declare the variable/function in the first place, as well as when he fails to run the cell that contains the underlying declaration.

IndexError

Raised when a sequence subscript is out of range. – Python Official Docs

Simply, developers face this error when they try to access a non-existent cell in a sequence such as list, array, or dataframe.

As can be seen in the provided code example, the message associated to IndexErrorwould include useful details to remove the fault. Generally, the message reports the requested index and the size of the axis to which the cell belongs.

KeyError

Raised when a mapping key is not found in the set of existing keys. – Python Official Docs

Primarily, developers encounter this error when they try to access a non-existent key in a dictionary or dataframe.

It seems simple, but we sometimes generate these keys automatically, and the names of the keys have little meaning for us. You can see an example of this error above, where I put the wrong condition in the while loop i <= ninstead of i < n.

AttributeError

Raised when an attribute reference or assignment fails. – Python Official Docs

Developers get this error whenever they try to access a non-existent variable/function from an imported object or module.

The above example of AttributeError illustrates the mis-use of scikit-learn Model object by calling accuracy instead of score . Once you face this type of error, you should re-verify the existence of the attribute in the imported object or module by inspecting the code files or the underlying library’s official docs.

TypeError

Raised when an operation or function is applied to an object of inappropriate type. – Python Official Docs

As indicated by its name, this error is mainly related to an incorrect type of variable/attribute/object. Nonetheless, it is often tricky to localize it.

  • First Cell: The error type and the message accompanying it are extremely clear. As I inferred a column based on other columns in a Pandas data frame, the resulting traceback includes a lot of sub-modules from the Pandas library. Consequently, it is wise to ignore all the Pandas’ inner functions and focus on the parts and lines of code we actually wrote. For instance, the code line, df['Savings']=df['Income']-df['Expenses'], where I inferred the ‘Savings’ column, is flagged within the traceback. To find the root cause, a simple check of the columns’ types would suffice.

Bonus Tip: When examining the traceback (Step 3), pay attention primarily to the function calls in your written modules.

  • Second Cell: Type errors that are less straightforward often appear in function or constructor calls. In this example, I made a misspelling error in an argument for the Model’s constructor. As a result, a traceback would be of little use to us. It would just help us identify the underlying function or object. The mistaken argument keyword would show up in the error message. Therefore, we need to look at the documentation or the source code of the function to find the error and fix it.
  • Third Cell: Here we call concatenate on two NumPy arrays, but concatenate expects a list of arrays to be concatenated, not two separate arguments. This is a common fault, as Python libraries might declare functions that deal with entries of the same variable’s type, using either multiple arguments (i.e., def func(**args)) or an argument of Iterabletype (i.e., def func(args:Iterable)). Clearly, the error message does not say much because the interpreter fails to convert the variables into the arguments types. Hence, it is necessary to verify the official library documentation or the source code of the underlying function to spot the root cause.

ValueError

Raised when an operation or function receives an argument that has the right type but an inappropriate value. – Python Official Docs

Last and not least, ValueErroris caused by an invalid value of an argument, which prevents the function from fulfilling the task.

The above code example shows misuse of a scikit-learn Model fit()function using inputs and targets with unmatched size. As the Model fit()is designed to handle such errors, the developer has included this exception to alert the users to the problem. Otherwise, some computations would fail with less obvious exceptions or the program remains silent and the results would be incorrect. In order to demonstrate the developer’s anticipation of ValueError, let us fix the first code example in this post by raising a ValueErrorthat appears when the user assigns the argument b to zero. The below snippet of code shows the required changes.

Final Words

I’m glad you made it all the way to the end. It would have been nice if I could finally have convinced you to stop copy-pasting the whole exception into Google and Stackoverflow search bars without any prior debugging in hopes of finding a ready-to-use fix. With practice, you will become able to quickly locate these bugs and fix them during the coding process, without having to go through other people’s shared errors.


Related Articles