Python Forum
[Basic] Simple debugging and how to read tracebacks
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Basic] Simple debugging and how to read tracebacks
#1

Debugging

Debug = Identify and remove errors

Simple rules to follow when posting on forums with regards to debugging your code:
A) If your posting more than about 10 lines of code to illustrate your problem, then you are going to annoy people. Why? Because no one wants to review a hundred lines of code to find the problem. You are the one that created the code, so you know it best.

B) Isolate the problem. How? The Traceback error. It will indicate the name of the error and the line the error is found. If the error is within the definition of a function, then post that section of the function. Sometimes you may also have to post the function call also. And always post the entire traceback error.

C) When explaining the error, get to the point. Don't make an essay about it.

D) Google it. Copy and paste the last line of the traceback error into google. Many times people have had the same error you have had. Where many people have answered and solved it already.

E) Get to know the standard Python exceptions and their meanings
https://docs.python.org/2/library/except...exceptions

F) Get an understanding of how Python generates exception messages in the first place
https://docs.python.org/2/tutorial/errors.html
 
Error:
Traceback: Traceback (most recent call last):   File "forum.py", line 2, in <module>     print(dicter.split()) AttributeError: 'dict' object has no attribute 'split'
This is a simple traceback error example.
1st line: Indicates that it is an error.
2nd line: shows the file or module the error is located in.
3rd line: is the exact line of code that caused the error.
4th line: Is the description of the error. It tells us it is an AttributeError, and that the object does not have that attribute.
Without even seeing the code the traceback error says it all in this case.

My line of code is valid, but I get a Traceback error for it?

Some Traceback errors are more complicated. The line of code that it shows as an error is valid. But you have to dig deeper. Think backwards. Let's take this code for example:
lister = ['a','b']
lister.pop()
print(lister[1])
Each line of code here is valid, but the manner that they are all together are not.
1st line: creates a list with 2 indexes in it, the strings 'a', and 'b'
2nd line: pops the last index out of the list
3rd line: attempts to print the second index of the list 'b'
Now we get the traceback:
Error:
Traceback (most recent call last):   File "forum.py", line 3, in <module>     print(lister[1]) IndexError: list index out of range
Now this does not mean that we typed the line of code print(lister[1]) wrong. It means that index of that list no longer exists. Even though we created that index, pop() took the last index of the list off and now we are trying to print an index of a list that no longer exists, giving us the error.
Even though this is simple, more complex problems can relate to this as the line of code that the error shows is not the direct problem.
The steps of finding this error:
1) Why is it not able to print?
2) What is it trying to print?
3) Any typo?
4) What makes the lists index out of range?
5) What affects that list previously of trying to print it?
Which will eventually lead you to the fact that you either need to not pop it, create a new index for it to print, or change the index of the list that it is trying to print.

Posting this on the forums
Now let's say you had this within a hundred lines of code, and you could not figure out the description of the traceback error. You would not post the entire hundred lines of code on the forums. Even as simple as it is, no one would look at it because no one would want to skim all those lines of code. You also don't want to just post the traceback and the line of the error that cause it, because then you are not giving enough information of what is affecting that list. In this situation posting the creation of the list, the line that affects the list, the print attempt of the list, and the traceback will give a small amount of code for the users to look at and find the answer to your problem, instead of them weaving through hundreds of lines of code to find it. Now you only posted a readble 7 lines of code. Post smart.
Recommended Tutorials:
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020