Python 类型对象 'datetime.datetime' 没有属性 'datetime'
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12906402/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
type object 'datetime.datetime' has no attribute 'datetime'
提问by Chris Frank
I have gotten the following error:
我收到以下错误:
type object 'datetime.datetime' has no attribute 'datetime'
类型对象 'datetime.datetime' 没有属性 '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 datetimeif that helps
from datetime import datetime如果有帮助,我导入了日期时间
Thanks
谢谢
采纳答案by John Lyon
Datetime is a module that allows for handling of dates, times and datetimes (all of which are datatypes). This means that datetimeis both a top-level module as well as being a type within that module. This is confusing.
Datetime 是一个允许处理日期、时间和日期时间(所有这些都是数据类型)的模块。这意味着datetime它既是顶级模块,又是该模块中的类型。这令人困惑。
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:
但是,如果您导入 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.
我怀疑您或您正在使用的模块之一已导入如下:
from datetime import datetime.
回答by waitingkuo
You should use
你应该使用
date = datetime(int(year), int(month), 1)
Or change
或者换
from datetime import datetime
to
到
import datetime
回答by RouR
For python 3.3
对于python 3.3
from datetime import datetime, timedelta
futuredate = datetime.now() + timedelta(days=10)
回答by M. Paul
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)
回答by Robino
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_datetimeinstead ofdatereduces confusion since there is already adatein the datetime module (datetime.date). - The module and the class (both called
datetime) do not shadow each other.
- 调用该变量
my_datetime而不是date减少混淆,因为datedatetime 模块 (datetime.date) 中已经存在 a 。 - 模块和类(均称为
datetime)不会相互遮蔽。
回答by Kamaldeep Singh
I found this to be a lot easier
我发现这要容易得多
from dateutil import relativedelta
relativedelta.relativedelta(end_time,start_time).seconds
回答by Jay Agrawal
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)
回答by Audrey Mengue
I run into the same error maybe you have already imported the module by using only import datetimeso change form datetime import datetimeto only import datetime. It worked for me after I changed it back.
我遇到了同样的错误,也许您已经使用 only 导入了模块,import datetime因此更改 form datetime import datetime为 only import datetime。我改回来后它对我有用。

