proffreda

Dealing with Errors when Running Python Code

Jun 23rd, 2016
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.20 KB | None | 0 0
  1. The Python Call Stack
  2.  
  3. 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.
  4.  
  5. Traceback (most recent call last):
  6. File "test.py", line 25, in ?
  7. triangle()
  8. File "test.py", line 12, in triangle
  9. inc_total_height()
  10. File "test.py", line 8, in inc_total_height
  11. total_height = total_height + height
  12. UnboundLocalError: local variable 'total_height' referenced before assignment
  13.  
  14. 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.
  15.  
  16. Carefully Read the Error messages:
  17.  
  18. Traceback (most recent call last):
  19. File "nx_error.py", line 41, in <module>
  20. print friends_of_friends(rj, myval)
  21. File "nx_error.py", line 30, in friends_of_friends
  22. f = friends(graph, user)
  23. File "nx_error.py", line 25, in friends
  24. return set(graph.neighbors(user))#
  25. File "/Library/Frameworks/…/graph.py", line 978, in neighbors
  26. return list(self.adj[n])
  27. TypeError: unhashable type: 'list'
  28.  
  29.  
  30. List of all exceptions (errors):
  31. http://docs.python.org/2/library/exceptions.html#bltin-exceptions
  32.  
  33. Two other resources, with more details about a few of the errors:
  34. http://inventwithpython.com/appendixd.html
  35. http://www.cs.arizona.edu/people/mccann/errors-python
  36.  
  37. Simple Debugging Tools
  38.  
  39. print statements:
  40. shows what’s happening with variables and whether there’s a problem or not and does not stop execution
  41.  
  42. assert statements:
  43. Raises an exception if some condition is not met and does nothing if everything works
  44. Example: assert len(rj.edges()) == 16
  45. Should use assert statements liberally! And Not just for debugging!
  46.  
  47. input statements:
  48. statements that prompt users for input can be used to stop execution.
  49.  
  50.  
  51. Import Errors
  52.  
  53. >>> from urllib2 import urlopen
  54. Traceback (most recent call last):
  55. File "<stdin>", line 1, in <module>
  56. ImportError: No module named 'urllib2'
  57.  
  58. Solution:
  59. 1. Googled :
  60. ImportError: No module named 'urllib2'
  61.  
  62. 2. From Top Ranked StackOverflow Answer we found:
  63. 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
  64.  
  65. So you should instead be saying
  66. from urllib.request import urlopen
  67. html = urlopen("http://www.google.com/")
  68. print(html)
  69.  
  70. 3. Deeper Issues: When import name is executed the interpreter searches for a file named name.py in several locations
  71. Current working directory given by os.getcwd(); System path given by variable sys.path;
  72. sys.path will include a list of directories specified by environment variable called PYTHONPATH
  73. Script being run should not have the same name as a python standard library.
Advertisement
Add Comment
Please, Sign In to add comment