python - 从具有相同名称的脚本导入已安装的包引发 "AttributeError: module has no attribute"或 ImportError 或 NameError

标签 python python-import python-module shadowing

我有一个名为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: partially initialized module 'requests' has no attribute 'get' (most likely due to a circular 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: cannot import name 'get' from partially initialized module 'requests' (most likely due to a circular import) (/Users/me/dev/rough/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

使用 star-import 然后使用该功能会引发 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

最佳答案

发生这种情况是因为您的本地模块名为 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]> .

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

相关文章:

python - 无法解析 "import as"情况 ("AttributeError module x has no attribute y")

Python 3 : Why am I getting an AttributeError?

python - 在另一个 python 项目中使用 python 库的自定义分支的最佳方法是什么?

python - 嵌入python模块错误

python - Tensorflow 1.8.0 : Wide and Deep Model results are not stable. 随机种子不起作用

python - 使用 pandas read_csv 函数将 'true' 保留为字符串,将 'True' 保留为 bool 值

python - 如何在 Windows 7 中将 fontforge 导入 python

python - 强制 python 使用旧版本的模块(比我现在安装的)

python - 为什么 python 的 sqlite3 模块试图从我的项目目录中导入日期时间?

Python 单元测试结果文件