Python 3.2无法导入 urllib2(Import Error: No module name urllib2)

我正在使用 Windows 系统,我得到了这个错误:

ImportError: No module named urllib2

我认为 这个是 Linux 的解决方案,但是如何在 Windows 中设置它呢?

我使用的是 Python 3.2,在 LiB 文件夹中看不到 urllib2

185204 次浏览

In python 3 urllib2 was merged into urllib. See also another Stack Overflow question and the urllib PEP 3108.

To make Python 2 code work in Python 3:

try:
import urllib.request as urllib2
except ImportError:
import urllib2
    import urllib2

Traceback (most recent call last):

File "", line 1, in

    import urllib2

ImportError: No module named 'urllib2' So urllib2 has been been replaced by the package : urllib.request.

Here is the PEP link (Python Enhancement Proposals )

http://www.python.org/dev/peps/pep-3108/#urllib-package

so instead of urllib2 you can now import urllib.request and then use it like this:

    >>>import urllib.request


>>>urllib.request.urlopen('http://www.placementyogi.com')

Original Link : http://placementyogi.com/articles/python/importerror-no-module-named-urllib2-in-python-3-x

PYTHON 3

import urllib.request


wp = urllib.request.urlopen("http://example.com")


pw = wp.read()


print(pw)

PYTHON 2

import urllib


import sys


wp = urllib.urlopen("http://example.com")


for line in wp:


sys.stdout.write(line)

While I have tested both the Codes in respective versions.