Import error: No module name urllib2
As stated in the urllib2
documentation:
The
urllib2
module has been split across several modules in Python 3 namedurllib.request
andurllib.error
. The2to3
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
Find Out Who Is Logged in on Linux Using Python
Compare Two Files for Differences in Python
Python 3.4.3 Modules Installation in Linux Error
How to Protect My Python Scripts on Raspberry Pi
No Such File or Directory "Limits.H" When Installing Pillow on Alpine Linux
Python Script Not Working via Cron
Why Does Loading the Libc Shared Library Have "'Libraryloader' Object Is Not Callable" Error
Using Python Subprocess.Call() to Launch an Ncurses Process
How to Direct Output to a File When There Are Utf-8 Characters
How to Open Process Again in Linux Terminal
How Remove Camera Preview to Raspberry Pi