问题:需要为Python 3.5.1安装urllib2

我正在为Mac运行Python 3.5.1。我想使用urllib2模块。我尝试安装它,但被告知它已被拆分成Python 3 urllib.requesturllib.error用于Python 3。

我的命令(现在不在框架bin目录中运行,因为它不在我的路径中):

sudo ./pip3 install urllib.request

返回此:

Could not find a version that satisfies the requirement urllib.request (from versions: )
No matching distribution found for urllib.request

在尝试一口气安装之前,我遇到了同样的错误urllib2

I’m running Python 3.5.1 for Mac. I want to use urllib2 module. I tried installing it but I was told that it’s been split into urllib.request and urllib.error for Python 3.

My command (running from the framework bin directory for now because it’s not in my path):

sudo ./pip3 install urllib.request

Returns this:

Could not find a version that satisfies the requirement urllib.request (from versions: )
No matching distribution found for urllib.request

I got the same error before when I tried to install urllib2 in one fell swoop.


回答 0

警告:安全性研究发现 PyPI上多个中毒的软件包,包括名为的软件包urllib,安装后会“打电话回家”。如果您pip install urllib在2017年6月之后使用了一段时间,请尽快删除该软件包

您不能,也不需要。

urllib2是Python 2中包含的库的名称。您可以改用Python 3中包含的urllib.request。该urllib.request库的工作方式与urllib2在Python 2中的工作方式相同。由于已经包含该库,因此您无需安装它。

如果您正在遵循的教程告诉您使用方法,urllib2那么您会发现遇到更多问题。您的教程是针对Python 2编写的,而不是针对Python 3编写的。找到其他教程,或者安装Python 2.7并继续该版本的教程。您会发现urllib2该版本随附。

或者,安装该requests以获得更高级别且更易于使用的API。它可以在Python 2和3上使用。

WARNING: Security researches have found several poisoned packages on PyPI, including a package named urllib, which will ‘phone home’ when installed. If you used pip install urllib some time after June 2017, remove that package as soon as possible.

You can’t, and you don’t need to.

urllib2 is the name of the library included in Python 2. You can use the urllib.request library included with Python 3, instead. The urllib.request library works the same way urllib2 works in Python 2. Because it is already included you don’t need to install it.

If you are following a tutorial that tells you to use urllib2 then you’ll find you’ll run into more issues. Your tutorial was written for Python 2, not Python 3. Find a different tutorial, or install Python 2.7 and continue your tutorial on that version. You’ll find urllib2 comes with that version.

Alternatively, install the requests library for a higher-level and easier to use API. It’ll work on both Python 2 and 3.


回答 1

根据文档

注意 urllib2模块已拆分为Python 3中名为urllib.request和的多个模块urllib.error。将源转换为Python 3时,2to3工具将自动适应导入。

因此,似乎无法做您想做的事,但您可以使用中的适当python3函数urllib.request

Acording to the docs:

Note 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 it appears that it is impossible to do what you want but you can use appropriate python3 functions from urllib.request.


回答 2

在Python 3中,urllib2被两个名为urllib.request和的内置模块代替urllib.error

来源改编


因此,替换为:

import urllib2

有了这个:

import urllib.request as urllib2

In Python 3, urllib2 was replaced by two in-built modules named urllib.request and urllib.error

Adapted from source


So replace this:

import urllib2

With this:

import urllib.request as urllib2

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。