python urllib 错误 - AttributeError: 'module' 对象没有属性 'request' [英] python urllib error - AttributeError: 'module' object has no attribute 'request'

查看:44
本文介绍了python urllib 错误 - AttributeError: 'module' 对象没有属性 'request'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying out a tutorial code which fetches the html code form a website and prints it. I'm using python 3.4.0 on ubuntu. The code:

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

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

I saw previous solutions and tried them, I also tried importing only urllib but it still doesn't work. The error message displayed is as shown:

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'

推荐答案

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

This looks like a nasty coincidence.

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

这里发生了什么?

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

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

urllib.request tries to import http.client, which tries to import email.parser, which tries to import email.feedparser, which tries to import email.message, which tries to import email.utils, which tries to import email.charset, which tries to import email.quoprimime.

email.quoprimime 尝试导入 string,期望它是 标准 Python string 模块——但由于当前工作目录优先于标准 Python 库目录,它会找到你的 string.py 而是尝试导入它.

email.quoprimime tries to import string, expecting it to be the standard Python string module—but since the current working directory has priority over the standard Python library directories, it finds your string.py instead and tries to import that.

在导入您的 string.py 时,您尝试导入 urllib.request.由于 urllib.request 仍在导入,因此您会返回一个没有 request 属性的骨架 urllib.

When importing your string.py, you try to import urllib.request. Since urllib.request is still being imported, you get back a skeleton urllib without a request attribute yet.

因为您导入的 string.py 然后失败,因为它找不到 request 属性,异常开始传播.

Because your imported string.py then fails because it can’t find the request attribute, the exception starts propagating back up.

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

But wait, there’s more! Since there was an error during an import, Ubuntu tries to be helpful by seeing if you’re missing a dpkg package. If so, it could say "hey, it looks like you’re missing this module; want to apt-get it?" So the mechanism for looking up the appropriate package is activated…

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

…but the module for looking up the appropriate package itself depends on urllib.request, so it tries to import it, and again fails…

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

In short, because you picked string.py as a file name, you overrode the standard string module, which broke a lot of other modules, and even broke the module that was supposed to be helpful when you were missing a module, causing a whole lot of havoc. Fortunately the solution is easy: rename your script.

这篇关于python urllib 错误 - AttributeError: 'module' 对象没有属性 'request'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆