Skip to content
Home » AttributeError: ‘module’ object has no attribute ‘urlopen’

AttributeError: ‘module’ object has no attribute ‘urlopen’

To solve AttributeError: ‘module’ object has no attribute ‘urlopen’ error follow below methods.

ERROR LOG

Traceback (most recent call last):  
    File "C:\Users\Sergio.Tapia\Documents\NetBeansProjects\DICParser\src\WebDownload.py", line 3, in <module>
     file = urllib.urlopen("http://www.python.org")
AttributeError: 'module' object has no attribute 'urlopen'

How to solve AttributeError: ‘module’ object has no attribute ‘urlopen’ ?

Refer the given methods to solve the issue.

Method 1:

For Python 2.x

import urllib.request

with urllib.request.urlopen("http://www.python.org") as url:
    s = url.read()
    print(s)

Method 2 :

For both Python 2 and python3 :

import sys

if sys.version_info[0] == 3:
    from urllib.request import urlopen
else:

    from urllib import urlopen


# Your code where you can use urlopen
with urlopen("http://www.python.org") as url:
    s = url.read()

print(s)

Hope the above solution works.

Also read :

django.core.exceptions.ImproperlyConfigured: Error loading psycopg module: No module named psycopg