Python 3.7中的ModuleNotFound

问题描述

我在cmd提示符下收到此错误:没有名为“ urllib.request”的模块 我已经安装了pip并仍然请求软件包

import urllib.request,urllib.parse,urllib.error
fhand = urllib.request.open('https://data.pr4e.org/romeo.txt')
for line in fhand:
    print(line.decode().strip())

解决方法

您可以使用以下代码:

import urllib
urllib.urlopen(url)

针对您的用例

from urllib import request,parse,error
fhand =request.urlopen('https://data.pr4e.org/romeo.txt')
for line in fhand:
    print(line.decode().strip())

我已经看到Python 3已经贬值了urllib本身,并用包括urllib.request在内的许多库替换了它,如果上面的方法不起作用,我已经找到了一个破解工具

尝试使用urllib2

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

此行应该可以代替urlopen:

from urllib2 import urlopen

在Python 2.7中测试

,

有两个独立的库:

  1. 标准库软件包urllib提供了urllib.request.urlopen()(不是open

由于这是Python的一部分,因此pip不需要安装任何内容。

但是您的代码不应因导入错误而失败,Python 3.7的示例以相同的方式导入模块:https://docs.python.org/3.7/library/urllib.request.html#examples

请更新您的问题,并提供有关该错误的更多信息。

  1. 受欢迎的高级套餐requests具有requests.get()

您写道,您已使用pip安装了此软件包。没必要。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...