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

‘module’ object has no attribute ‘urlopen’

AttributeError: ‘module’ object has no attribute ‘urlopen’ error occurs when you are trying to “use urlopen from the urllib module in Python 3.”

How to fix it?

To fix the AttributeError: ‘module’ object has no attribute ‘urlopen’ error, import urlopen from the urllib.request module in Python 3.

To use urlopen in Python 3, you should import it like this:

from urllib.request import urlopen

If you are using Python 2, you can import like this:

from urllib import urlopen

For example:

import urllib.request as ur

s = ur.urlopen("https://appdividend.com")
sl = s.read()
print(sl)

Conclusion

The “urlopen” method is not an object of “urllib” in Python 3. To access this function, import “urllib.request” into your code and reference the “urllib.request.urlopen()” function.

Related posts

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

ImportError: cannot import name ‘url’ from ‘django.conf.urls’

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.