Import not working as expected but found a workaround


#1

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!


#2

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

The following also works:

import urllib.request