1

When i wrote:

import urllib

fhand = urllib.request.urlopen('http://data.pr4e.org/romeo.txt')
for line in fhand:
    print(line.decode().strip())

The above function returns no attribute name 'request' found

but works when importing all the functions needed:

import urllib.request,urllib.parse,urllib.error

fhand = urllib.request.urlopen('http://data.pr4e.org/romeo.txt')
for line in fhand:
    print(line.decode().strip())

Any suggestions ?

Nitish
  • 565
  • 4
  • 16

1 Answers1

0

From this answer: When you use an import statement it always searches the actual module path (and/or sys.modules); it doesn't make use of module objects in the local namespace that exist because of previous imports.

So if you want to use any object of the urllib, you need to type in the actual objects you want to use:

import urllib.request

You can also do that:

from urllib import request

fhand = request.urlopen('http://data.pr4e.org/romeo.txt')

Quentin Laillé
  • 125
  • 1
  • 12