gpt4 book ai didi

python - 找不到模块属性,即使它存在

转载 作者:太空宇宙 更新时间:2023-11-03 14:28:05 25 4
gpt4 key购买 nike

内置 http 模块有一个非常奇怪的 python 问题。导入成功,但是http.client属性不存在?它在 python 文档中,我的兄弟完美地运行了代码。但它对我不起作用,无论是在我的 Linux 笔记本电脑还是我的 Mac 上!这个问题已经有几天了,真的需要解决它!

准确的错误信息是:

except (http.client.ResponseNotReady, http.client.CannotSendRequest, http.client.BadStatusLine) as e:
AttributeError: 'module' object has no attribute 'client'

当删除这个异常时,这个文件中有更多的异常,所以我不能简单地删除那个异常

因为我找不到这个问题的根源,所以我开始探测http模块文件夹并找到client.py

在 python 解释器中输入 help(http) 时,它会这样响应:

PACKAGE CONTENTS
client
cookiejar
cookies
server

FILE
/opt/local/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/http/__init__.py

好吧,我知道帮助命令信息是自动生成的,那些包可能已被删除或其他什么。所以我去了 http 目录并做了一个“ls -l”给了我这个:

-rw-r--r--   1 root  wheel     38  6 Apr 21:29 __init__.py
drwxr-xr-x 12 root wheel 408 4 May 21:29 __pycache__
-rw-r--r-- 1 root wheel 44644 6 Apr 21:29 client.py
-rw-r--r-- 1 root wheel 75973 6 Apr 21:29 cookiejar.py
-rw-r--r-- 1 root wheel 20360 6 Apr 21:29 cookies.py
-rw-r--r-- 1 root wheel 44788 6 Apr 21:29 server.py

这就是我一直在寻找的client.py!它甚至有很多代码,所以它不能访问吗?

也不可能仅仅尝试在 python 解释器中访问它!

>>> import http
>>> http.client
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'client'

提前致谢!

最佳答案

您导入了 http。但是你是否也导入了http.client

这是必要的,因为它取决于包的 __init__.py 是否导入子模块。

关于python - 找不到模块属性,即使它存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16859544/

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