python urllib usage
Asked Answered
R

3

5

I imported two libraries urllib and from urllib.request import urlopen.

The second one is contained in the first

When I went over the code and tried to remove the from urllib.request import urlopen line , I got this message:

opnerHTMLnum = urllib.request.build_opener()
AttributeError: 'module' object has no attribute 'request'

When I restore the from urllib.request import urlopen line the code runs .

Can anyone explain why?

import re
#import http.cookiejar
import os.path
#import time
#import urllib3
import urllib
from urllib.request import urlopen
import sys
import smtplib
from email.mime.text import MIMEText

# ...

    opnerHTMLnum = urllib.request.build_opener()
Ravo answered 24/10, 2012 at 16:8 Comment(1)
from urllib.request import urlopen gives ImportError: No module named request on Python 2.7. Which version of Python are you using?Salary
E
7

You are confusing python3 package urllib.request with Python2.7 one which is urllib2. Please don't do that. Python3 and Python2 are libraries are different. All you may want is urllib2 from python2

import urllib2
from urllib2 import Request
req = Request("yoururl")
res = urllib2.urlopen(req)
Erikaerikson answered 24/10, 2012 at 16:13 Comment(2)
I think the OP got confused with the tags; if he really was on Python 2.7, there would have been an import error, not the behaviour stated in the question.Aecium
Good catch, I was following this example: nltk.org/book/ch03.html and I didn't realise the examples referred to python3. My env is Python 2.7. +1 voted, thanks!Abernathy
A
2

The urllib package is just that, a package. It's __init__.py does not import urllib.request and thus you cannot simply reach urllib.request by only importing urllib. It is intended as a namespace only.

Import urllib.request instead.

Aecium answered 24/10, 2012 at 16:11 Comment(0)
R
2

Both import X and from X import Y perform an import of whatever module or package X that is given.

In this case, urllib is a package. When you import urllib, then the package itself is imported, and you get a reference to it, but any submodules are not imported (in this case). When you do from urllib.request import ..., Python actually imports the entire module urllib.request, but then picks out the names that you asked for and gives you references to them.

If you aren't using urlopen, then you could also easily do import urllib.request and get the same result.

Ridgley answered 24/10, 2012 at 16:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.