Python ImportError:没有名为datetime的模块

Aka*_*nde 2 python python-2.7

您好,我在尝试简单的日期时间导入时收到错误.

ImportError:没有名为datetime的模块

我在git控制台中尝试以下代码.

>>> from datetime.datetime import strptime
Run Code Online (Sandbox Code Playgroud)

我试过重新安装python,它似乎不起作用.我究竟做错了什么?

Rya*_*ing 6

datetime.datetimedatetime模块内的一个类datetime.您无法导入类的方法,这实际上是您要执行的操作.相反,你可以:

from datetime import datetime
datetime.strptime(...)
Run Code Online (Sandbox Code Playgroud)

或者以你想要的方式"提取"方法:

strptime = datetime.strptime
Run Code Online (Sandbox Code Playgroud)

虽然左侧的名字=完全取决于您.

该错误消息本身是从第二次来datetimedatetime.datetime,而不是第一个.