gpt4 book ai didi

urllib - python urllib 错误 - AttributeError : 'module' object has no attribute 'request'

转载 作者:行者123 更新时间:2023-12-01 12:38:27 27 4
gpt4 key购买 nike

我正在尝试一个教程代码,它从一个网站获取 html 代码并打印出来。我在 ubuntu 上使用 python 3.4.0。代码:

import urllib.request
page = urllib.request.urlopen("http://www.brainjar.com/java/host/test.html")
text = page.read().decode("utf8")
print(text)

我看到了以前的解决方案并尝试了它们,我也尝试过只导入 urllib 但它仍然不起作用。显示的错误信息如图所示:

Traceback (most recent call last):
File "string.py", line 1, in <module>
import urllib.request
File "/usr/lib/python3.4/urllib/request.py", line 88, in <module>
import http.client
File "/usr/lib/python3.4/http/client.py", line 69, in <module>
import email.parser
File "/usr/lib/python3.4/email/parser.py", line 12, in <module>
from email.feedparser import FeedParser, BytesFeedParser
File "/usr/lib/python3.4/email/feedparser.py", line 27, in <module>
from email import message
File "/usr/lib/python3.4/email/message.py", line 15, in <module>
from email import utils
File "/usr/lib/python3.4/email/utils.py", line 40, in <module>
from email.charset import Charset
File "/usr/lib/python3.4/email/charset.py", line 15, in <module>
import email.quoprimime
File "/usr/lib/python3.4/email/quoprimime.py", line 44, in <module>
from string import ascii_letters, digits, hexdigits
File "/media/saiwal/D89602199601F930/Documents/Copy/codes/python/headfirst/string.py", line 2, in <module>
page = urllib.request.urlopen("http://www.brainjar.com/java/host/test.html")
AttributeError: 'module' object has no attribute 'request'
Error in sys.excepthook:
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/apport_python_hook.py", line 63, in apport_excepthook
from apport.fileutils import likely_packaged, get_recent_crashes
File "/usr/lib/python3/dist-packages/apport/__init__.py", line 5, in <module>
from apport.report import Report
File "/usr/lib/python3/dist-packages/apport/report.py", line 21, in <module>
from urllib.request import urlopen
File "/usr/lib/python3.4/urllib/request.py", line 88, in <module>
import http.client
File "/usr/lib/python3.4/http/client.py", line 69, in <module>
import email.parser
File "/usr/lib/python3.4/email/parser.py", line 12, in <module>
from email.feedparser import FeedParser, BytesFeedParser
File "/usr/lib/python3.4/email/feedparser.py", line 27, in <module>
from email import message
File "/usr/lib/python3.4/email/message.py", line 15, in <module>
from email import utils
File "/usr/lib/python3.4/email/utils.py", line 40, in <module>
from email.charset import Charset
File "/usr/lib/python3.4/email/charset.py", line 15, in <module>
import email.quoprimime
File "/usr/lib/python3.4/email/quoprimime.py", line 44, in <module>
from string import ascii_letters, digits, hexdigits
File "/media/saiwal/D89602199601F930/Documents/Copy/codes/python/headfirst/string.py", line 2, in <module>
page = urllib.request.urlopen("http://www.brainjar.com/java/host/test.html")
AttributeError: 'module' object has no attribute 'request'

Original exception was:
Traceback (most recent call last):
File "string.py", line 1, in <module>
import urllib.request
File "/usr/lib/python3.4/urllib/request.py", line 88, in <module>
import http.client
File "/usr/lib/python3.4/http/client.py", line 69, in <module>
import email.parser
File "/usr/lib/python3.4/email/parser.py", line 12, in <module>
from email.feedparser import FeedParser, BytesFeedParser
File "/usr/lib/python3.4/email/feedparser.py", line 27, in <module>
from email import message
File "/usr/lib/python3.4/email/message.py", line 15, in <module>
from email import utils
File "/usr/lib/python3.4/email/utils.py", line 40, in <module>
from email.charset import Charset
File "/usr/lib/python3.4/email/charset.py", line 15, in <module>
import email.quoprimime
File "/usr/lib/python3.4/email/quoprimime.py", line 44, in <module>
from string import ascii_letters, digits, hexdigits
File "/media/saiwal/D89602199601F930/Documents/Copy/codes/python/headfirst/string.py", line 2, in <module>
page = urllib.request.urlopen("http://www.brainjar.com/java/host/test.html")
AttributeError: 'module' object has no attribute 'request'

最佳答案

这看起来像是一个令人讨厌的巧合。

TL;DR:不要将脚本命名为 string.py


那么这里发生了什么?

  1. 您正在尝试导入 urllib.request

  2. urllib.request 尝试导入 http.client,后者尝试导入 email.parser,后者尝试导入 email.feedparser,它试图导入 email.message,它试图导入 email.utils,它试图导入 email.charset ,它会尝试导入 email.quoprimime

  3. email.quoprimime 尝试导入 string,期望它是 the standard Python string module — 但由于当前工作目录优先于标准 Python 库目录,它会找到您的 string.py 并尝试导入它。

  4. 导入string.py 时,您尝试导入urllib.request。由于 urllib.request 仍在导入,您会得到一个骨架 urllib,但还没有 request 属性。

  5. 因为您导入的 string.py 因找不到 request 属性而失败,异常开始向上传播。

  6. 等等,还有更多!由于在导入过程中出现错误,Ubuntu 试图通过查看您是否缺少 dpkg 包来提供帮助。如果是这样,它可能会说“嘿,看起来你缺少这个模块;想要 apt-get 吗?”于是查找合适包的机制就被激活了……

  7. ...但是查找相应包的模块本身依赖于 urllib.request,因此它尝试导入它,但再次失败...

简而言之,因为您选择了 string.py 作为文件名,所以您覆盖了 the standard string module ,它破坏了很多其他模块,甚至破坏了当你缺少一个模块时应该有用的模块,造成了很大的破坏。幸运的是,解决方案很简单:重命名您的脚本。

关于urllib - python urllib 错误 - AttributeError : 'module' object has no attribute 'request' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27367618/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com