'ImportError: No module named error when importing urllib.error

I am just getting my feet wet in the art of webscraping and I am following the tutorials from this source. For some reason I cannot import the error module from 'urllib' to handle exceptions. Since this is a built-in library, I am confused as to why this is an issue.

from urllib import urlopen
from urllib.error import HTTPError
from urllib.error import URLError

yields the error

ImportErrorTraceback (most recent call last)
<ipython-input-1-30b72b3bf2ea> in <module>()
      1 from urllib import urlopen
----> 2 from urllib.error import HTTPError
      3 from urllib.error import URLError

I have tried the same code with another IDE (IntelliJ) and it works as expected leading me to believe that this could be an issue with Google Colab itself. Could someone weight in and possibly help me find a solution to this problem.

I am new to programming, so if this is a juvenile question or if this is not the appropriate place for this question, I apologize in advance.

P.S. I have double checked that the runtime is Python 3



Solution 1:[1]

Your problem is in

from urllib import urlopen

Right way to import urlopen is from urllib.request

from urllib.request import urlopen

Docs

Solution 2:[2]

Just try this:

from urllib.request import urlopen

Always remember to try to search the docs of a particular library it helps a lot.

Solution 3:[3]

You are trying to run this code using Python 2. Use Python 3 and it will work.

Python 2:

>>> from urllib.error import HTTPError
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named error
>>> 

Python 3:

>>> from urllib.error import HTTPError
>>>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Druta Ruslan
Solution 2 Pujan Mehta
Solution 3 Michael