12.2: Runtime Errors
- Page ID
Once your program is syntactically correct, Python can compile it and at least start running it. What could possibly go wrong?
My program does absolutely nothing.
This problem is most common when your file consists of functions and classes but does not actually invoke anything to start execution. This may be intentional if you only plan to import this module to supply classes and functions.
If it is not intentional, make sure that you are invoking a function to start execution, or execute one from the interactive prompt. Also see the “Flow of Execution” section below.
My program hangs.
If a program stops and seems to be doing nothing, it is “hanging.” Often that means that it is caught in an infinite loop or infinite recursion.
- If there is a particular loop that you suspect is the problem, add a
Run the program. If you get the first message and not the second, you’ve got an infinite loop. Go to the “Infinite Loop” section below.
- Most of the time, an infinite recursion will cause the program to run for a while and then produce a “RuntimeError: Maximum recursion depth exceeded” error. If that happens, go to the “Infinite Recursion” section below.
If you are not getting this error but you suspect there is a problem with a recursive method or function, you can still use the techniques in the “Infinite Recursion” section.
- If neither of those steps works, start testing other loops and other recursive functions and methods.
- If that doesn’t work, then it is possible that you don’t understand the flow of execution in your program. Go to the “Flow of Execution” section below.
If you think you have an infinite loop and you think you know what loop is causing the problem, add a
while x > 0 and y < 0 : # do something to x # do something to y print "x: ", x print "y: ", y print "condition: ", (x > 0 and y < 0)
Now when you run the program, you will see three lines of output for each time through the loop. The last time through the loop, the condition should be
false. If the loop keeps going, you will be able to see the values of
y, and you might figure out why they are not being updated correctly.
Most of the time, an infinite recursion will cause the program to run for a while and then produce a
Maximum recursion depth exceeded error.
If you suspect that a function or method is causing an infinite recursion, start by checking to make sure that there is a base case. In other words, there should be some condition that will cause the function or method to return without making a recursive invocation. If not, then you need to rethink the algorithm and identify a base case.
If there is a base case but the program doesn’t seem to be reaching it, add a
Flow of Execution
If you are not sure how the flow of execution is moving through your program, add
foo is the name of the function.
Now when you run the program, it will print a trace of each function as it is invoked.
When I run the program I get an exception.
If something goes wrong during runtime, Python prints a message that includes the name of the exception, the line of the program where the problem occurred, and a traceback.
The traceback identifies the function that is currently running, and then the function that invoked it, and then the function that invoked that, and so on. In other words, it traces the sequence of function invocations that got you to where you are. It also includes the line number in your file where each of these calls occurs.
The first step is to examine the place in the program where the error occurred and see if you can figure out what happened. These are some of the most common runtime errors:
- You are trying to use a variable that doesn’t exist in the current environment. Remember that local variables are local. You cannot refer to them from outside the function where they are defined.
- There are several possible causes:
- You are trying to use a value improperly. Example: indexing a string, list, or tuple with something other than an integer.
- There is a mismatch between the items in a format string and the items passed for conversion. This can happen if either the number of items does not match or an invalid conversion is called for.
- You are passing the wrong number of arguments to a function or method. For methods, look at the method definition and check that the first parameter is
self. Then look at the method invocation; make sure you are invoking the method on an object with the right type and providing the other arguments correctly.
- You are trying to access an element of a dictionary using a key that the dictionary does not contain.
- You are trying to access an attribute or method that does not exist. Check the spelling! You can use
dirto list the attributes that do exist.
If an AttributeError indicates that an object has
NoneType, that means that it is
None. One common cause is forgetting to return a value from a function; if you get to the end of a function without hitting a
returnstatement, it returns
None. Another common cause is using the result from a list method, like
sort, that returns
- The index you are using to access a list, string, or tuple is greater than its length minus one. Immediately before the site of the error, add a
The Python debugger (
pdb) is useful for tracking down Exceptions because it allows you to examine the state of the program immediately before the error. You can read about
pdb at http://docs.python.org/2/library/pdb.html.
I added so many print statements I get inundated with output.
One of the problems with using
To simplify the output, you can remove or comment out
To simplify the program, there are several things you can do. First, scale down the problem the program is working on. For example, if you are searching a list, search a small list. If the program takes input from the user, give it the simplest input that causes the problem.
Second, clean up the program. Remove dead code and reorganize the program to make it as easy to read as possible. For example, if you suspect that the problem is in a deeply nested part of the program, try rewriting that part with simpler structure. If you suspect a large function, try splitting it into smaller functions and testing them separately.
Often the process of finding the minimal test case leads you to the bug. If you find that a program works in one situation but not in another, that gives you a clue about what is going on.
Similarly, rewriting a piece of code can help you find subtle bugs. If you make a change that you think shouldn’t affect the program, and it does, that can tip you off.