问题:类型对象“ datetime.datetime”没有属性“ datetime”

我收到以下错误:

类型对象“ datetime.datetime”没有属性“ datetime”

在下一行:

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

有人知道错误的原因吗?

我导入日期时间from datetime import datetime是否有帮助

谢谢

I have gotten the following error:

type object ‘datetime.datetime’ has no attribute ‘datetime’

On the following line:

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

Does anybody know the reason for the error?

I imported datetime with from datetime import datetime if that helps

Thanks


回答 0

日期时间是一个允许处理日期,时间和日期时间(所有都是数据类型)的模块。这意味着datetime它既是顶级模块,又是该模块中的一种类型。这很混乱。

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

>>> 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:

>>> 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

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)

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)

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


回答 1

对于python 3.3

from datetime import datetime, timedelta
futuredate = datetime.now() + timedelta(days=10)

For python 3.3

from datetime import datetime, timedelta
futuredate = datetime.now() + timedelta(days=10)

回答 2

你应该用

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

或改变

from datetime import datetime

import datetime

You should use

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

Or change

from datetime import datetime

to

import datetime

回答 3

您实际上应该将模块导入其自己的别名中

import datetime as dt
my_datetime = dt.datetime(year, month, day)

与其他解决方案相比,以上优点如下:

  • 调用变量my_datetime而不是date减少混乱,因为datedatetime模块中已经有一个(datetime.date)。
  • 模块和类(都称为datetime)不会相互遮挡。

You should really import the module into its own alias.

import datetime as dt
my_datetime = dt.datetime(year, month, day)

The above has the following benefits over the other solutions:

  • Calling the variable my_datetime instead of date reduces confusion since there is already a date in the datetime module (datetime.date).
  • The module and the class (both called datetime) do not shadow each other.

回答 4

如果您使用过:

from datetime import datetime

然后只需将代码编写为:

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

但是,如果您使用过:

import datetime

那么只有你可以写:

date = datetime.datetime(int(2005), int(5), 1)

If you have used:

from datetime import datetime

Then simply write the code as:

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

But if you have used:

import datetime

then only you can write:

date = datetime.datetime(int(2005), int(5), 1)

回答 5

我发现这要容易得多

from dateutil import relativedelta
relativedelta.relativedelta(end_time,start_time).seconds

I found this to be a lot easier

from dateutil import relativedelta
relativedelta.relativedelta(end_time,start_time).seconds

回答 6

我遇到了同样的错误,也许您已经通过仅使用导入了模块,import datetime所以将其更改 form datetime import datetime为only import datetime。我改回来后对我有用。

I run into the same error maybe you have already imported the module by using only import datetime so change form datetime import datetime to only import datetime. It worked for me after I changed it back.


回答 7

from datetime import datetime
import time
from calendar import timegm
d = datetime.utcnow()
d = d.strftime("%Y-%m-%dT%H:%M:%S.%fZ")
utc_time = time.strptime(d,"%Y-%m-%dT%H:%M:%S.%fZ")
epoch_time = timegm(utc_time)
from datetime import datetime
import time
from calendar import timegm
d = datetime.utcnow()
d = d.strftime("%Y-%m-%dT%H:%M:%S.%fZ")
utc_time = time.strptime(d,"%Y-%m-%dT%H:%M:%S.%fZ")
epoch_time = timegm(utc_time)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。