Import not working as expected but found a workaround

For the code:

import urllib
fhand = urllib.request.urlopen(‘URL’)

NameError Traceback (most recent call last)
in ()
3 from urllib import *
4
----> 5 fhand = request.urlopen(‘URL’)
6
7 for line in fhand:

NameError: name ‘request’ is not defined

But when I changed it to:

import urllib.request
fhand = urllib.request.urlopen(‘URL’)

it works!

1 Like

Both urllib and request are modules so to use request you will have to import that explicitly.

The following also works:

import urllib.request