类型对象'datetime.datetime'没有属性'datetime' [英] type object 'datetime.datetime' has no attribute 'datetime'

查看:2636
本文介绍了类型对象'datetime.datetime'没有属性'datetime'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在执行一个django教程,我收到以下错误:

I'm following a django tutorial, and I have gotten the following error:


类型对象'datetime.datetime'没有属性'datetime'

type object 'datetime.datetime' has no attribute 'datetime'

在下列行:

date = datetime.datetime(int(year), int(month), 1)



有没有人知道错误的原因?

Does anybody know the reason for the error?

我从datetime import datetime 导入datetime与如果帮助

I imported datetime with from datetime import datetime if that helps

感谢

推荐答案

Datetime是一个允许处理的模块的日期,时间和数据时间(所有这些都是数据类型)。这意味着 datetime 既是顶级模块,也是该模块中的一种类型。这是令人困惑的。

Datetime is a module that allows for handling of dates, times and datetimes (all of which are datatypes). This means that datetime is both a top-level module as well as being a type within that module. This is confusing.

您的错误可能基于模块的混乱命名,您或您正在使用的模块已导入。 >

Your error is probably based on the confusing naming of the module, and what either you or a module you're using has already imported.

>>> import datetime
>>> datetime
<module 'datetime' from '/usr/lib/python2.6/lib-dynload/datetime.so'>
>>> datetime.datetime(2001,5,1)
datetime.datetime(2001, 5, 1, 0, 0)

但是,如果您导入datetime.datetime:

But, if you import datetime.datetime:

>>> from datetime import datetime
>>> datetime
<type 'datetime.datetime'>
>>> datetime.datetime(2001,5,1) # You shouldn't expect this to work 
                                # as you imported the type, not the module
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: type object 'datetime.datetime' has no attribute 'datetime'
>>> datetime(2001,5,1)
datetime.datetime(2001, 5, 1, 0, 0)

我怀疑您或您正在使用的其中一个模块导入如下:
from datetime import datetime

I suspect you or one of the modules you're using has imported like this: from datetime import datetime.

这篇关于类型对象'datetime.datetime'没有属性'datetime'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆