How do I get more info about a python error?

python error types
python try except: print error
python catch all exceptions
python try except any error
python get exception message
python try except continue
python try without except
python except exception as e

I'm trying to run a python script on Ubuntu 16.04; the script runs fine on Ubuntu 14.04, but I keep getting kind of a vague object has no attribute error.

It seems this exception class is still active so not sure what the problem is. I've installed python-urllib3 and python3-urllib3 (even though python on the 16.04 system is a symlink to python-2.7) with no luck.

This is my error and line 507 from the code. Any way to get more info about the error?

Traceback (most recent call last):
  File "./", line 507, in <module>
    except urllib.error.URLError as e:
AttributeError: 'module' object has no attribute 'error'
import urllib
import re
import json
import sys
import getopt
# line 507
except urllib.error.URLError as e:
   print "fail: ", e.reason

update: As noted by @a_guest. Had to make this change. Why it runs fine on 14.04 I don't know. Maybe this was the difference bewteen 2.7.6 and 2.7.12:

except urllib2.error.URLError as e:

You linked the documentation for Python 3 however you seem to be using Python 2. urllib on Python 2 doesn't have that error module, just as the error states.

urllib2 on the other hand has this class, so you can use urllib2.URLError instead.

Errors and Exceptions¶. Until now error messages haven't been more than mentioned, but if you have tried out the examples you have  Give your Python skills a complete workout at the Python Gymnasium! This bundle is packed with best practice projects, exercises to flex your coding muscles.

That error means that the urllib module contains nothing called error. My urllib doesn't have an error submodule.

In Python, an error can be a syntax error or an exception. In this article, you will see what an exception is and how it differs from a syntax error. After that, you will​  Find python course on KensaQ updates its results daily to help you find what you are looking for. Search Now!

Note that Ubuntu 16.04 /usr/bin/python is a symlink to python2. If you want to use python 3.x, you should change the first line of your script from




(Note: the "#!" at the beginning of this line is called "shebang".)

Alternatively you can create a virtual environment so that when you activate it, it will run python 3.

You can preserve the stacktrace (and error value) with sys.exc_info() , but this is way more error prone and has compatibility problems between Python 2 and 3,  This Python For Beginners Course Teaches You The Python Language Fast. Understand how to create your own Python programs.

On the other hand, the Python interpreter handles errors by terminating immediately and printing an error message, so that programmers can address issues as  Assertions in Python An assertion is a sanity-check that you can turn on or turn off when you are done with your testing of the program. The easiest way to think of an assertion is to liken it to a raise-if statement (or to be more accurate, a raise-if-not statement).

The try block lets you test a block of code for errors. The except block lets you handle the error. The finally block lets you execute code, regardless of the result of  Python errors and exceptions Python errors and exceptions. Every programmer encounters errors, both those who are just beginning, and those who have been programming for years. Encountering errors and exceptions can be very frustrating at times, and can make coding feel like a hopeless endeavour.

of examples of how to use HTML, CSS, JavaScript, SQL, PHP, Python, Bootstrap, Java and XML. Raise an error and stop the program if x is lower than 0:. Use traceback.print_exc() to print the current exception to standard error, just like it would be printed if it remained uncaught, or traceback.format_exc() to get the same output as a string. You can pass various arguments to either of those functions if you want to limit the output, or redirect the printing to a file-like object.

  • use traceback
  • @jedwards - Mostly. 14.04 ships with 2.7.6 while 16.04 ships with python 2.7.12
  • alternatively, run the script with Python 3.
  • Maybe specific to python3? The urllib module has been split into parts and renamed in Python 3 to urllib.request, urllib.parse, and urllib.error
  • Thanks. The symlink to /usr/bin/python on 14.04 is also python2 (/usr/bin/python -> python2.7) so pretty sure I'm not trying to run it on python3
  • @ServerFault If you change the first line of the file as I suggest in my question, you can run the script with python 3.
  • Thanks, I'll keep that in mind however I've not made the provisions in the script to switch to python3 yet (eg: wrapping print() strings in parenthesis, etc) .