gpt4 book ai didi

python - 从(或附近)具有相同名称的脚本导入库会引发 "AttributeError: module has no attribute"或 ImportError 或 NameError

转载 作者:太空宇宙 更新时间:2023-11-03 21:23:11 24 4
gpt4 key购买 nike

我有一个名为 requests.py 的脚本,需要使用第三方 requests 包。该脚本要么无法导入包,要么无法访问其功能。

为什么这不起作用,如何修复它?

尝试简单导入然后使用该功能会导致 AttributeError:

import requests

res = requests.get('http://www.google.ca')
print(res)
Traceback (most recent call last):
File "/Users/me/dev/rough/requests.py", line 1, in <module>
import requests
File "/Users/me/dev/rough/requests.py", line 3, in <module>
requests.get('http://www.google.ca')
AttributeError: module 'requests' has no attribute 'get'

在较新版本的 Python 中,错误消息改为 AttributeError:部分初始化的模块“requests”没有属性“get”(很可能是由于循环导入)

使用特定名称的 from-import 会导致 ImportError:

from requests import get

res = get('http://www.google.ca')
print(res)
Traceback (most recent call last):
File "requests.py", line 1, in <module>
from requests import get
File "/Users/me/dev/rough/requests.py", line 1, in <module>
from requests import get
ImportError: cannot import name 'get'

在较新版本的 Python 中,错误消息改为 ImportError: 无法从部分初始化的模块“requests”导入名称“get”(很可能是由于循环导入)(/Users/me/dev/粗略/requests.py)

对包内的模块使用 from-import 会导致不同的 ImportError:

from requests.auth import AuthBase
Traceback (most recent call last):
File "requests.py", line 1, in <module>
from requests.auth import AuthBase
File "/Users/me/dev/rough/requests.py", line 1, in <module>
from requests.auth import AuthBase
ImportError: No module named 'requests.auth'; 'requests' is not a package

使用星号导入然后使用该功能会引发NameError:

from requests import *

res = get('http://www.google.ca')
print(res)
Traceback (most recent call last):
File "requests.py", line 1, in <module>
from requests import *
File "/Users/me/dev/rough/requests.py", line 3, in <module>
res = get('http://www.google.ca')
NameError: name 'get' is not defined
<小时/>

如果您故意将模块命名为与现有模块相同,并且想要处理这种情况,请参阅 How can I import from the standard library, when my project has a module with the same name? (How can I control where Python looks for modules?)

最佳答案

发生这种情况是因为您的本地模块名为 requests.py隐藏已安装的requests您正在尝试使用的模块。当前目录前缀为 sys.path ,因此本地名称优先于安装的名称。

出现这种情况时,一个额外的调试技巧是仔细查看回溯,并意识到相关脚本的名称与您尝试导入的模块相匹配:

注意您在脚本中使用的名称:

File "/Users/me/dev/rough/requests.py", line 1, in <module>

您尝试导入的模块:requests

将模块重命名为其他名称以避免名称冲突。

Python 可能会生成 requests.pyc文件 requests.py 旁边文件(在 Python 3 中的 __pycache__ 目录中)。重命名后也将其删除,因为解释器仍会引用该文件,从而重现错误。然而,pyc文件在 __pycache__如果 py 不应该影响您的代码文件已被删除。

在示例中,将文件重命名为 my_requests.py ,删除 requests.pyc ,再次运行成功打印 <Response [200]> .

<小时/>

注意:这种情况不仅仅发生在将文件命名为您尝试导入的模块时。如果您将文件命名为与直接导入的模块导入的模块相同的名称,也可能会发生这种情况。例如,有一个名为 copy.py 的文件并试图import pandas从那里,将给出

ImportError: cannot import name 'copy' from 'copy'

那是因为pandas进口copy 。这里没有神奇的解决方案,因为您无法知道世界上所有模块的名称,但经验法则是尝试使模块的名称尽可能唯一,并在出现此类错误时尝试更改名称。

关于python - 从(或附近)具有相同名称的脚本导入库会引发 "AttributeError: module has no attribute"或 ImportError 或 NameError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54067654/

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