Python 导入 urllib.request,ImportError: No module named request

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

Import urllib.request, ImportError: No module named request

pythonimporterror

提问by Raj

I am trying to import urllib.request for python 2.7.10 on PyCharm 4.5.4 on Window 10 but getting the error "ImportError: No module named request".

我正在尝试在 Window 10 上的 PyCharm 4.5.4 上为 python 2.7.10 导入 urllib.request,但收到错误“ImportError: No module named request”。

回答by Ojas Srivastava

The urllib.request modules have been deprecated .. just use

urllib.request 模块已被弃用 .. 只需使用

import urllib

And for your function if you were earlier writing say

对于你的功能,如果你之前写过的话

urllib.request.urlretrieve

Now you just write

现在你只要写

urllib.urlretrieve

回答by Bu-gae Park

I have also faced the same error and Googled to solve it. urlib.requestis for Python 3.0.

我也遇到了同样的错误,并用谷歌来解决它。 urlib.request适用于 Python 3.0。

You may use the code below:

您可以使用以下代码:

import urllib
urllib.urlopen(url)

回答by theninjagreg

You'll get this error if you try running a python 3 file with python 2.

如果您尝试使用 python 2 运行 python 3 文件,则会收到此错误。

回答by Akash Kandpal

Try to use this in Python3

尝试在 Python3 中使用它

try:
    x = urllib.request.urlopen('https://www.google.com/search?q=test')
    print(x.read())

except Exception as e:
    print(str(e))

回答by bob marti

Use > Path\easy_install.exerequests if you have a windows machine, where easy_installcan be found in your Python*\Scriptsfolder, if it was installed. (Note Path\easy_install.exe is an example, mine is C:\Python32\Scripts\easy_install.exe)

Path\easy_install.exe如果您有一台 Windows 机器,请使用 > requests,如果安装了它,easy_install可以在Python*\Scripts文件夹中找到。(注意 Path\easy_install.exe 是一个例子,我的是 C:\Python32\Scripts\easy_install.exe)

If you don't have easy install and are running on a windows machine, you can get it here: http://www.lfd.uci.edu/~gohlke/pythonlibs/#distribute

如果你没有简单的安装并且在 Windows 机器上运行,你可以在这里得到它:http: //www.lfd.uci.edu/~gohlke/pythonlibs/#distribute

If you manually want to add a library to a windows machine, you can download the compressed library, uncompress it, and then place it into the Lib folder of your python path.

如果你想手动添加一个库到windows机器,你可以下载压缩库,解压,然后放到你python路径的Lib文件夹中。

OR

或者

You need to install pipfirst and then install django-requestusing pip

您需要先安装pip,然后django-request使用pip安装

pip install django-request

also install,

也安装,

python setup.py install

then import

然后导入

from urllib.request import urlopen

Helpful Tips:to chech this

有用的提示:检查这个