【问题标题】:AttributeError: module 'datetime' has no attribute 'date' on import yamlAttributeError:模块“日期时间”在导入 yaml 时没有属性“日期”
【发布时间】:2020-04-18 13:42:36
【问题描述】:

我的代码行之一是

import yaml

使用pip install pyyaml安装在python 3.7上

出现以下错误

Traceback(最近一次调用最后一次):

文件 "C:/code/EPMD/Kodex/Applications/EPMD-Software/Sandbox/peer_sharing_send_rate.py", 第 1 行,在 从 TestPeer.TestPeerChangingSendRate 导入 TestPeerChangingSendSpeed 文件“C:\code\EPMD\Kodex\Applications\EPMD-Software\TestPeer\TestPeerChangingSendRate.py”,

第 1 行,在 从 .TestPeer 导入 TestPeer 文件“C:\code\EPMD\Kodex\Applications\EPMD-Software\TestPeer\TestPeer.py”,

第 4 行,在 从 BaseProcess.ZmqPeerClass 导入 ZmqPeer 文件“C:\code\EPMD\Kodex\Applications\EPMD-Software\BaseProcess\ZmqPeerClass.py”,

第 2 行,在 从 .ZmqPublisherClass 导入 ZmqPublisher 文件“C:\code\EPMD\Kodex\Applications\EPMD-Software\BaseProcess\ZmqPublisherClass.py”,

第 10 行,在 从 。导入 ZmqProcessClass 文件“C:\code\EPMD\Kodex\Applications\EPMD-Software\BaseProcess\ZmqProcessClass.py”,

第 5 行,在 从 .ConfigBaseClass 导入 ConfigBase 文件“C:\code\EPMD\Kodex\Applications\EPMD-Software\BaseProcess\ConfigBaseClass.py”,

第 3 行,在 导入yaml 文件“C:\code\EPMD\Kodex\venv\lib\site-packages\yaml__init__.py”,第 9 行,在 从 .dumper 导入 * 文件“C:\code\EPMD\Kodex\venv\lib\site-packages\yaml\dumper.py”,第 6 行,在 从 .representer 导入 * 文件“C:\code\EPMD\Kodex\venv\lib\site-packages\yaml\representer.py”,行

263,在 SafeRepresenter.add_representer(datetime.date, AttributeError: module 'datetime' has no attribute 'date'

如何让import yaml 工作?

【问题讨论】:

  • 您是否在错误消息中显示的任何目录中碰巧有一个datetime.py 文件?如果你这样做,那么重命名它
  • 哇,我永远也抓不到那个!如果你能解释你的思维过程,我会很高兴
  • 我通过答案添加了更多

标签: python python-3.x pyyaml


【解决方案1】:

您可能在错误消息中显示的目录之一(很可能是C:/code/EPMD/Kodex/Applications/EPMD-Software/Sandbox/)中有一个名为datetime.py 的文件,如果这样做,则需要将其重命名为不会影响任何其他Python 模块的名称。

原因是它掩盖了实际的datetime 模块,因为当前工作目录中的文件/目录/模块优先于安装在site-packages 目录中的模块(内置和安装的模块位于其中)。如果两个位置都包含a 模块,那么import a 将导入本地a 模块,而不是从site-packages 导入(可能)预期的a 模块。

yaml\representer.py 执行import datetime 时,它导入了没有date 属性的datetime.py 文件/模块,这就是为什么在后来尝试使用datetime.date 时会引发AttributeError

【讨论】:

    猜你喜欢
    • 2019-02-28
    • 2020-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多