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

41

我有一个带有以下导入的脚本:

from datetime import datetime

还有一段代码,我在其中调用:

datetime.fromisoformat(duedate)

很遗憾,当我使用Python 3.6的实例运行脚本时,控制台返回以下错误:

AttributeError: type object 'datetime.datetime' has no attribute 'fromisoformat'

我尝试在两个anaconda实例(3.7和3.8)上运行它,结果非常顺利。我认为这可能是一个导入问题,因此我尝试将datetime.py从anaconda / Lib复制到脚本目录中,但没有成功。

datetime.py 明确包含类 datetime 和方法 fromisoformat,但仍然似乎没有链接。我甚至尝试显式链接 datetime.py 文件,但出现了相同的错误:

parent_dir = os.path.abspath(os.path.dirname(__file__))
vendor_dir = os.path.join(parent_dir, 'libs')
sys.path.append(vendor_dir+os.path.sep+"datetime.py")

你能帮我吗?我的创意用完了...

6个回答

58

这里的问题实际上是在Python 3.7以下版本中没有fromisoformat方法,你可以在文档中清楚地看到。

Return a date corresponding to a date_string given in the format YYYY-MM-DD:
>>>

>>> from datetime import date
>>> date.fromisoformat('2019-12-04')
datetime.date(2019, 12, 4)

This is the inverse of date.isoformat(). It only supports the format YYYY-MM-DD.

New in version 3.7.

5
你知道老版本的Python 3有什么替代方案吗? - Akinn
也许这个链接是你正在寻找的。 - gold_cy
isoformat() 似乎具有相反的效果,因为它转换为ISO8601格式。我会寻找替代解决方案。顺便说一句,谢谢。 - Akinn

15

我遇到了同样的问题,找到了这个:

https://pypi.org/project/backports-datetime-fromisoformat/

>>> from datetime import date, datetime, time
>>> from backports.datetime_fromisoformat import MonkeyPatch
>>> MonkeyPatch.patch_fromisoformat()

>>> datetime.fromisoformat("2014-01-09T21:48:00-05:30")
datetime.datetime(2014, 1, 9, 21, 48, tzinfo=-05:30)

>>> date.fromisoformat("2014-01-09")
datetime.date(2014, 1, 9)

>>> time.fromisoformat("21:48:00-05:30")
datetime.time(21, 48, tzinfo=-05:30)

轻松搞定。


运行得很好,但是在datetime.datetime中是否有类似于timedelta的函数?你如何在这里增加日期? - Suraj
它的工作方式就像你所期望的那样。由于您仍然导入“datetime”并只更新“fromisoformat”,因此其余部分保持不变。 - Rudertier

12

您应该将datetime.fromisoformat('2021-08-12')重构为以下代码所示的datetime.strptime

In [1]: from datetime import datetime                                                                                                                                                          

In [2]: datetime.strptime("2021-08-08", "%Y-%m-%d")                                                                                                                                           
Out[2]: datetime.datetime(2021, 8, 8, 0, 0)

3

Python 3.6及以下版本没有fromisoformat()方法 - 如其他文档所述 - 两个datetime.fromisoformat (文档) 和 date.fromisoformat (文档) 都不可用。

您可以使用我编写的以下代码在Python 3.6中实现此功能。我不喜欢为我几乎不使用的函数安装额外的依赖项 - 在我的情况下,我只在测试中使用它。

Python3.6及以下版本

from datetime import datetime

time_expected = datetime.now()
time_actual = datetime.strptime(time_actual.isoformat(), "%Y-%m-%dT%H:%M:%S.%f")
assert time_actual == time_expected

Python3.7+

from datetime import datetime

time_expected = datetime.now()
time_actual = datetime.fromisoformat(time_expected.isoformat())
assert time_actual == time_expected

0
我遇到了这个问题,或者说是同样的错误信息,尽管我正在使用Python 3.11.6。在我的情况下,真正的问题是对导入名称和类名的混淆。具体如下:
(venv)rob@robuntuflex:~/proj/pracMonFront$ python Python 3.11.6 (main, Oct 8 2023, 05:06:43) [GCC 13.2.0] on linux 输入 "help"、"copyright"、"credits" 或 "license" 获取更多信息。 >>> import datetime >>> datetime.fromisoformat("2023-10-07T10:20:38") Traceback (most recent call last): File "", line 1, in AttributeError: module 'datetime' has no attribute 'fromisoformat' >>> datetime.datetime.fromisoformat("2023-10-07T10:20:38") datetime.datetime(2023, 10, 7, 10, 20, 38)

希望对你有帮助。
/rob

-19

你不应该使用 from datetime import datetime 而是使用 import datetime,这样可以避免模块名称混淆的问题。 现在你可以使用 datetime.fromisoformat(duedate)


我尝试了,但是出现了类似的错误:AttributeError: module 'datetime' has no attribute 'fromisoformat'。 - Akinn
2
它适用于Python 3.7和3.8,因为fromisoformat在Python 3.7中被添加。 - Ranga

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接