【发布时间】: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