AttributeError: module 'datetime' has no attribute 'time'

AttributeError: module 'datetime' has no attribute 'time'

我正在使用 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'

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

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

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