Import Error: No Module Name Urllib2

Import error: No module name urllib2

As stated in the urllib2 documentation:

The urllib2 module has been split across several modules in Python 3 named urllib.request and urllib.error. The 2to3 tool will automatically adapt imports when converting your sources to Python 3.

So you should instead be saying

from urllib.request import urlopen
html = urlopen("http://www.google.com/").read()
print(html)

Your current, now-edited code sample is incorrect because you are saying urllib.urlopen("http://www.google.com/") instead of just urlopen("http://www.google.com/").

`No module named 'urllib2' - how do i use it in Python so I can make a Request

There is no urllib2 in python3; see this question for more details. (The short version of the backstory here is that Python2 and Python3 are entirely different types of flying altogether; not all stdlib libraries in Py2 are available in Py3.)

Instead, try urllib (similar API);

from urllib import request

You can hit the urllib documentation here, which may help.

ImportError: No module named 'urllib2' Python 3

check StackOverflow Link

import urllib.request
url = "http://www.google.com/"
request = urllib.request.Request(url)
response = urllib.request.urlopen(request)
print (response.read().decode('utf-8'))

I can't import urllib2

urllib2 was merged with urllib in python 3. Is a standard library in python 2 but not in 3.

Try this if you want to use the urlopen method

from urllib.request import urlopen
html = urlopen("http://www.google.com/")
print(html)

Also:

The urllib2 module has been split across several modules in Python 3.0
named urllib.request and urllib.error. The 2to3 tool will
automatically adapt imports when converting your sources to 3



Related Topics



Leave a reply



Submit