Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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 ?
- triangle()
- File "test.py", line 12, in triangle
- inc_total_height()
- 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.
- Carefully Read the Error messages:
- Traceback (most recent call last):
- File "nx_error.py", line 41, in <module>
- print friends_of_friends(rj, myval)
- File "nx_error.py", line 30, in friends_of_friends
- f = friends(graph, user)
- File "nx_error.py", line 25, in friends
- return set(graph.neighbors(user))#
- File "/Library/Frameworks/…/graph.py", line 978, in neighbors
- return list(self.adj[n])
- TypeError: unhashable type: 'list'
- List of all exceptions (errors):
- http://docs.python.org/2/library/exceptions.html#bltin-exceptions
- Two other resources, with more details about a few of the errors:
- http://inventwithpython.com/appendixd.html
- http://www.cs.arizona.edu/people/mccann/errors-python
- Simple Debugging Tools
- print statements:
- shows what’s happening with variables and whether there’s a problem or not and does not stop execution
- assert statements:
- Raises an exception if some condition is not met and does nothing if everything works
- Example: assert len(rj.edges()) == 16
- Should use assert statements liberally! And Not just for debugging!
- input statements:
- statements that prompt users for input can be used to stop execution.
- Import Errors
- >>> from urllib2 import urlopen
- Traceback (most recent call last):
- File "<stdin>", line 1, in <module>
- ImportError: No module named 'urllib2'
- Solution:
- 1. Googled :
- ImportError: No module named 'urllib2'
- 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 saying
- from urllib.request import urlopen
- html = urlopen("http://www.google.com/")
- print(html)
- 3. Deeper Issues: When import name is executed the interpreter searches for a file named name.py in several locations
- Current working directory given by os.getcwd(); System path given by variable sys.path;
- sys.path will include a list of directories specified by environment variable called PYTHONPATH
- Script being run should not have the same name as a python standard library.
Advertisement
Add Comment
Please, Sign In to add comment