"datetime":“模块"对象没有属性“现在" [英] "datetime": 'module' object has no attribute 'now'
问题描述
我有一个使用from datetime import datetime
方法运行的脚本.第一次运行脚本时,第一次调用datetime.now()
会引发错误.如果我再次运行它,它将顺利通过其余部分.
I have a script that I run using the from datetime import datetime
method. The first time that I run the script, the first call to datetime.now()
throws the error. If I run it again it will sail through the rest without a problem.
这是一个代码段:
from datetime import datetime
tot_time = datetime.now() # It bonks on this line
推荐答案
如果在from datetime import datetime
之后执行import *
,则可能会用另一个模块中的普通import datetime
覆盖from
导入.
If you are doing an import *
after your from datetime import datetime
, you could be overriding your from
import with a plain import datetime
from another module.
找出是否是名称空间问题的一种方法是执行以下操作:
from datetime import datetime as dt
.大概不会与另一个dt
发生冲突.
One way to find out if it is a namespace issue is to do the following:
from datetime import datetime as dt
. Presumably, you won't collide with another dt
.
这篇关于"datetime":“模块"对象没有属性“现在"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!