【问题标题】:Python datetime module problemsPython 日期时间模块问题
【发布时间】:2020-05-09 23:51:09
【问题描述】:

我在 python3.7 中使用datetime 模块。 当我从终端运行它时,它会抛出一个错误:

File "/home/user/Desktop/Yazılım/datetime.py", line 2, in <module>
x=datetime.datetime.now()
AttributeError: module 'datetime' has no attribute 'now'

【问题讨论】:

  • 您是否导入了 datetime 模块?在我的工作(py -3.7)。

标签: python-3.x linux datetime visual-studio-code terminal


【解决方案1】:

正如你命名你的文件 datetime.py 从外部调用它可能会导致混乱。

您应该将其重命名为 my_datetime.py 之类的其他名称

你还需要在使用前导入日期时间

import datetime
datetime.datetime.now()

【讨论】:

    【解决方案2】:

    尝试改变

    import datetime
    

    from datetime import datetime
    

    学分:https://stackoverflow.com/a/19231474/12770606

    【讨论】:

      猜你喜欢
      • 2010-10-28
      • 2014-09-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-19
      • 2011-11-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多