[英]ModuleNotFound in python 3.7
i am getting this error in cmd prompt: No module named 'urllib.request' i have installed pip and requests packages still i am getting this我在 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())
You may use the code below:您可以使用以下代码:
import urllib
urllib.urlopen(url)
for your use case为您的用例
from urllib import request, parse, error
fhand =request.urlopen('https://data.pr4e.org/romeo.txt')
for line in fhand:
print(line.decode().strip())
I've seen Python 3 has depreciated urllib itself and replaced it with a number of libraries including urllib.request,if above doesn't work, I have found a hack for我已经看到 Python 3 贬低了 urllib 本身并将其替换为包括 urllib.request 在内的许多库,如果上面不起作用,我找到了一个黑客
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测试于 Python 2.7
There are two independent libraries:有两个独立的库:
urllib
provides urllib.request.urlopen()
(not open
)urllib
提供了urllib.request.urlopen()
(未open
) As this is part of the Python, nothing needs to be installed with pip
.由于这是 Python 的一部分,因此无需安装
pip
。
But your code should not fail with import error, the examples for Python 3.7 import the module in the same way: https://docs.python.org/3.7/library/urllib.request.html#examples但是您的代码不应因导入错误而失败,Python 3.7 的示例以相同的方式导入模块: https://docs.python.org/3.7/library/urllib.request.html#examples
Please update your question and provide more information about the error.请更新您的问题并提供有关该错误的更多信息。
requests
has requests.get()
:requests
有requests.get()
: You wrote, that you have installed this package with pip
.你写道,你已经安装了这个 package 和
pip
。 This was not necessary.这是没有必要的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.