gpt4 book ai didi

python - "type object ' datetime.datetime ' has no attribute ' datetime '"即使是"import datetime"

转载 作者:太空宇宙 更新时间:2023-11-04 08:11:23 30 4
gpt4 key购买 nike

我在 AppEngine 上收到 type object 'datetime.datetime' has no attribute 'datetime' 错误,提示日期时间类型,但我的导入是 import datetime . 其他 文件中有from datetime import datetime,但我认为这不会影响此文件?

我检查过,没有“意外”重新导入。我检查了我的 AppEngine 日志,它在 2 天前才开始发生。

我使用的是 2.7 运行时。

编辑:这是导致错误的行(注意我使用的是 import datetime,而不是 from datetime import datetime)

task.due_at = datetime.datetime.strptime(date, '%Y-%m-%d %I:%M%p')

编辑:堆栈跟踪

type object 'datetime.datetime' has no attribute 'datetime'
Traceback (most recent call last):
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.1/webapp2.py", line 570, in dispatch
return method(*args, **kwargs)
File "/base/data/home/apps/s~wmphighrise/1.373696587983821954/myapp/handler/decorators.py", line 22, in wrapper
return fn(*args, **kwargs)
File "/base/data/home/apps/s~wmphighrise/1.373696587983821954/myapp/handler/api/main.py", line 1343, in post
task.due_at = datetime.datetime.strptime(date, '%Y-%m-%d %I:%M%p')
AttributeError: type object 'datetime.datetime' has no attribute 'datetime'

编辑 3:一些调试

注意:import datetime在最上面

#1st attempt
import datetime
class MyHandler():
def get(self):
logging.info(datetime) # => "<type 'datetime.datetime'>"

#2nd attempt
import datetime
class MyHandler():
def get(self):
import datetime # explicitly re-import the module
logging.info(datetime) # => "<module 'datetime' (built-in)>"

#3rd attempt
import datetime
class MyHandler():
def get(self):
logging.info(datetime) # => Throws UnboundLocalError: local variable 'datetime' referenced before assignment
# Is this normal? This is new to me.
import datetime
logging.info(datetime)

#4th attempt
import datetime
logging.info(datetime) # => "<module 'datetime' (built-in)>"
class MyHandler():
def get(self):
logging.info(datetime) # => "<type 'datetime.datetime'>"

有没有办法在当前文件或模块之外重新定义变量?因为我已经查看并查看了这个文件,根本没有重新定义。

编辑 4:

我已经ack"datetime =", "datetime=", "datetime.datetime =""datetime.datetime=",但没有重新分配的结果。我检查了过去 2 天的 git 日志,没有任何可以引入它的更改

最佳答案

你包含的代码对我有用,所以问题很可能是你用一个名为 datetime.datetime 对象隐藏了 datetime 模块日期时间。不认真,我是想打字的。

>>> import datetime
>>> datetime = datetime.datetime.now() # waves goodbye to datetime module!
>>> task.due_at = datetime.datetime.strptime(date, '%Y-%m-%d %I:%M%p')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'datetime.datetime' object has no attribute 'datetime'

关于python - "type object ' datetime.datetime ' has no attribute ' datetime '"即使是"import datetime",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21720678/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com