you're reading...

Dealing with Errors in Python

Common Errors and Debugging Python Programs

The Python Call Stack

As functions are called, their names are placed on the stack, and as they return, their names are removed. The Traceback presents us with the list of called functions (from the first called to the most recent called [most recent call last]), telling us the file where the call occurred, the line in that file, and the name of the function the call was made from if any (otherwise ‘?’). On the next line slightly indented it tells us the name of the function called.

Traceback (most recent call last):
File “test.py”, line 25, in ?
File “test.py”, line 12, in triangle
File “test.py”, line 8, in inc_total_height
total_height = total_height + height
UnboundLocalError: local variable ‘total_height’ referenced before assignment

From the above Traceback we see that execution started in the file test.py and proceeded to line 25, where the function ‘triangle’ was called. Within the function triangle, execution proceeded until line 12, where the function ‘inc_total_height’ was called. Within ‘inc_total_height’ and error occurred on line 8.


Simple Debugging Strategies

*****use print statements: useful for showing what’s happening with the state of variables and will not stop execution flow.

******assert statements: useful for raising an exception if some condition is not met and does nothing if everything works and assert statement evaluates to true.

Example: assert(len(rj.edges()) == 16)

Should use assert statements liberally! And Not just for debugging!

****input statements: useful for prompting users for input and can be used to stop execution.

Import Errors

Try the following from python prompt:

>>> from urllib2 import urlopen

Traceback (most recent call last):
File “<stdin>”, line 1, in <module>
ImportError: No module named ‘urllib2’

Google for a Solution:
Step 1. Googled :
“ImportError: No module named ‘urllib2’ ”


Step 2. From Top Ranked StackOverflow Answer we found:

The urllib2 module has been split across several modules in Python 3.0 named urllib.request and urllib.error. The 2to3 tool will automatically adapt imports when converting your sources to 3

So you should instead be writing:

>>> from urllib.request import urlopen
>>> html = urlopen(“http://www.google.com/&#8221;)
>>> print(html)

<http.client.HTTPResponse object at 0x103019cf8>

>>> type(html)

<class ‘http.client.HTTPResponse’>

>>> dir(html)

[‘__abstractmethods__’, + many more including read ]

>>> print(html.read())

Step 3. Some Deeper Issues: When import name is executed the interpreter searches for a file named name.py in several locations such as the

“Current Working Directory” given by os.getcwd(); the “System path” given by variable sys.path; sys.path will include a list of directories specified by environment variable called PYTHONPATH. To avoid conflicts modules being run should not have the same name as a python standard library. Finally, by using project directories and tool called virtualenv we can often avoid dealing with system path problems.



No comments yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: