Python 2.7.10 错误“从 urllib.request 导入 urlopen”没有名为 request 的模块

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31601238/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 10:17:13  来源:igfitidea点击:

Python 2.7.10 error "from urllib.request import urlopen" no module named request

pythonurllib

提问by Steve Maguire

I opened python code from github. I assumed it was python2.xand got the above error when I tried to run it. From the reading I've seen Python 3 has depreciated urllibitself and replaced it with a number of libraries including urllib.request.

我从github. 我认为是这样python2.x,当我尝试运行它时出现了上述错误。从阅读中我看到 Python 3 已经贬值了urllib,取而代之的是许多库,包括urllib.request.

It looks like the code was written in python 3 (a confirmation from someone who knows would be appreciated.) At this point I don't want to move to Python 3 - I haven't researched what it would do to my existing code.

看起来代码是用 python 3 编写的(知道的人的确认将不胜感激。)此时我不想转移到 Python 3 - 我还没有研究它会对我现有的代码做什么。

Thinking there should be a urllibmodule for Python 2, I searched Google (using "python2 urllib download") and did not find one. (It might have been hidden in the many answers since urllibincludes downloading functionality.) I looked in my Python27/libdirectory and didn't see it there. Can I get a version of this module that runs on Python27? Where and how?

认为应该有一个urllib模块Python 2,我搜索了谷歌(使用“python2 urllib下载”)并没有找到一个。(它可能隐藏在许多答案中,因为urllib包括下载功能。)我查看了我的Python27/lib目录,但没有在那里看到它。我可以获得在 上运行的此模块的版本Python27吗?在哪里以及如何?

回答by NevDev

Try using urllib2:

尝试使用 urllib2:

https://docs.python.org/2/library/urllib2.html

https://docs.python.org/2/library/urllib2.html

This line should work to replace urlopen:

这一行应该可以替代 urlopen:

from urllib2 import urlopen

Tested in Python 2.7 on Macbook Pro

在 Macbook Pro 上用 Python 2.7 测试

Try posting a link to the git in question.

尝试发布指向相关 git 的链接。

回答by Anand S Kumar

You are right the urlliband urllib2packages have been split into urllib.request, urllib.parseand urllib.errorpackages in Python 3.x. The latter packages do not exist in Python 2.x

你是对的urlliburllib2包在 Python 3.x 中被拆分为urllib.requesturllib.parseurllib.error包。后面的包在 Python 2.x 中不存在

From documentation-

文档-

The urllib module has been split into parts and renamed in Python 3 to urllib.request, urllib.parse, and urllib.error.

urllib 模块已被拆分为多个部分,并在 Python 3 中重命名为 urllib.request、urllib.parse 和 urllib.error。

From urllib2documentation-

来自urllib2文档-

The urllib2 module has been split across several modules in Python 3 named urllib.request and urllib.error.

urllib2 模块在 Python 3 中被拆分为多个名为 urllib.request 和 urllib.error 的模块。

So I am pretty sure the code you downloaded has been written for Python 3.x , since they are using a library that is only present in Python 3.x .

所以我很确定你下载的代码是为 Python 3.x 编写的,因为它们使用的库只存在于 Python 3.x 中。

There is a urllibpackage in python, but it does not have the requestsubpackage. Also, lets assume you do lots of work and somehow make requestsubpackage available in Python 2.x .

python中有urllib包,但是没有request子包。此外,假设您做了大量工作,并以某种方式使请求子包在 Python 2.x 中可用。

There is a very very high probability that you will run into more issues, there is lots of incompatibility between Python 2.x and Python 3.x , in the end you would most probably end up rewriting atleast half the code from github (and most probably reading and understanding the complete code from there).

您很有可能会遇到更多问题,Python 2.x 和 Python 3.x 之间存在很多不兼容性,最后您很可能最终会重写 github 中至少一半的代码(以及大多数可能从那里阅读和理解完整的代码)。

Even then there may be other bugs arising from the fact that some of the implementation details changed between Python 2.x to Python 3.x (As an example - list comprehension got its own namespace in Python 3.x)

即便如此,由于某些实现细节在 Python 2.x 到 Python 3.x 之间发生变化,可能会产生其他错误(例如 - 列表理解在 Python 3.x 中有自己的命名空间)

You are better off trying to download and use Python 3 , than trying to make code written for Python 3.x compatible with Python 2.x

您最好尝试下载和使用 Python 3 ,而不是尝试使为 Python 3.x 编写的代码与 Python 2.x 兼容

回答by Jonathan Holloway

Instead of using urllib.request.urlopen() remove request for python 2.

而不是使用 urllib.request.urlopen() 删除 python 2 的请求。

urllib.urlopen() you do not have to request in python 2.x for what you are trying to do. Hope it works for you. This was tested using python 2.7 I was receiving the same error message and this resolved it.

urllib.urlopen() 你不必在 python 2.x 中请求你要做什么。希望对你有效。这是使用 python 2.7 测试的,我收到了相同的错误消息,这解决了它。

回答by user3291138

For now, it seems that I could get over that by adding a ?after the URL.

现在,我似乎可以通过?在 URL 后面添加一个来解决这个问题。

回答by Maciej Osowski

from urllib.request import urlopen, Request

Should solve everything

应该解决一切

回答by boardrider

You can program defensively, and do your import as:

您可以进行防御性编程,并按以下方式进行导入:

try:
    from urllib.request import urlopen
except ImportError:
    from urllib2 import urlopen

and then in the code, just use:

然后在代码中,只需使用:

data = urlopen(MIRRORS).read(AMOUNT2READ)

回答by ceekay

Change

改变

from urllib.request import urlopen

from urllib.request import urlopen

to

from urllib import urlopen

from urllib import urlopen

I was able to solve this problem by changing like this. For Python2.7in macOS10.14

我能够通过这样改变来解决这个问题。对于Python2.7macOS10.14