【问题标题】:AttributeError: module 'datetime' has no attribute 'time' [duplicate]AttributeError:模块“日期时间”没有属性“时间”[重复]
【发布时间】:2019-02-28 20:50:36
【问题描述】:

我正在使用 Python 3.7 并尝试从库中学习 datetime 模块,但我得到了 AttributeError。代码如下:

import datetime

t = datetime.time(4, 20, 1)

# Let's show the different components
print(t)
print('hour  :', t.hour)
print('minute:', t.minute)
print('second:', t.second)
print('microsecond:', t.microsecond)
print('tzinfo:', t.tzinfo)

作为运行文件,我收到此错误AttributeError: module 'datetime' has no attribute 'time'

请帮助我找出哪里出错了。谢谢

【问题讨论】:

  • 您的工作目录中是否有一个名为“datetime.py”的文件?
  • 您的安装已损坏,或者如 Daniel 所述,您在某处有一个名为 datetime 的文件隐藏了内置模数。 print(datetime.__file__) 输出什么?
  • 根据 Python 3.7 文档,您的代码应该可以工作。 docs.python.org/3/library/datetime.html我猜你的问题有不同的原因。
  • 您的代码似乎可以工作:onlinegdb.com/SyHDJYDtX 很可能是您的 python 或项目文件的问题,正如其他人提到的那样
  • 你的代码可以工作,所以就像之前说的,你可能会检查你项目的其他文件。尝试将您的代码隔离在您的项目之外,并运行您的 py 文件。如果有效,请检查您的项目,如果无效,请检查您的 python conf

标签: python python-3.x runtime-error attributeerror python-datetime


【解决方案1】:

问题可能是你的 python 文件与 datetime 模块同名,也许你调用了你的程序文件 datetime.py

您需要重命名程序文件并删除路径中最终生成的任何 .pyc 文件,然后才能导入正确的日期时间模块。

【讨论】:

    猜你喜欢
    • 2020-04-18
    • 1970-01-01
    • 1970-01-01
    • 2020-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多