Server Fault
Server Fault

Reputation: 637

How do I get more info about a python error?

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 "./jsontest.py", line 507, in <module>
    except urllib.error.URLError as e:
AttributeError: 'module' object has no attribute 'error'
#!/usr/bin/python
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:

Upvotes: 1

Views: 537

Answers (3)

Code-Apprentice
Code-Apprentice

Reputation: 83537

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

#!/usr/bin/python

to

#!/usr/bin/python3

(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.

Upvotes: 0

a_guest
a_guest

Reputation: 36289

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.

Upvotes: 2

Sam Hartman
Sam Hartman

Reputation: 6499

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

Upvotes: 0

Related Questions