Python 从 urllib2 导入请求 - 无法导入名称请求

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29490562/
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 04:39:26  来源:igfitidea点击:

from urllib2 import request - cannot import name request

pythonpython-2.7modulerequesturllib2

提问by Niels Gorsel

So i am trying to write a program and i need the module named requests for this, since i am using python version 2.7.9.

所以我正在尝试编写一个程序,我需要名为 requests 的模块,因为我使用的是 python 版本 2.7.9。

i thought using the code:

我想使用代码:

from urllib2 import request

would do the job. Yet i get the following Error:

会做的工作。但是我收到以下错误:

Traceback (most recent call last):
  File "C:\Python27\NLTKHoofdstuk3.py", line 5, in <module>
    from urllib2 import request
ImportError: cannot import name request

I don't get it because i have urllib2 fully working on Python and the module request should be in the module urllib2 (i also don't get an Error when i type "import urllib2")

我不明白,因为我有 urllib2 完全在 Python 上工作,模块请求应该在模块 urllib2 中(当我输入“import urllib2”时我也没有收到错误)

So does anyone know why my module request is not working but my module urllib2 is

那么有谁知道为什么我的模块请求不起作用但我的模块 urllib2 是

回答by Ajay

Try using

尝试使用

from urllib2 import Request

instead of

代替

from urllib2 import request

Check the official documentation

查看官方文档

回答by trang hang

Using

使用

import urllib;

and then

进而

urllib.urllopen();

For Python 2.7you do not need importrequest.

因为Python 2.7你不需要import请求。

回答by keven.Song

try using:

尝试使用:

import urllib2

and use urllib2to instead of urllib.request

并使用urllib2代替urllib.request

looking at the document https://docs.python.org/2/library/urllib2.html#urllib2.urlopenmay help more.

查看文档https://docs.python.org/2/library/urllib2.html#urllib2.urlopen可能会有更多帮助。

回答by Abhishek Poudel

I am using Python 2.7 and encountered the same problem. I was able to solve it after using urllib3instead of urllibor urllib2.

我正在使用 Python 2.7 并遇到了同样的问题。使用urllib3而不是urllibor后我能够解决它urllib2

Here's an example screenshot of using urllib3working in Python 2.7: jupyter_Screenshot_for_urllib3

这是urllib3在 Python 2.7中使用的示例屏幕截图: jupyter_Screenshot_for_urllib3