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

问题描述 投票:0回答:5

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

from datetime import datetime

以及我调用的一段代码:

datetime.fromisoformat(duedate)

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

AttributeError:类型对象“datetime.datetime”没有属性“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")

你能帮我吗?我的想法已经结束了...

python datetime python-import
5个回答
58
投票

这里的问题实际上是

fromisoformat
在低于3.7的Python版本中不可用,你可以在这里的文档中看到明确的说明。

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.

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)

就像魅力一样。


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()
方法 - 正如其他文档中提到的 - both
datetime.fromisoformat
(docs) 和
date.fromisoformat
(docs) 不可用。

您可以使用我编写的代码在 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
Type "help", "copyright", "credits" or "license" for more information.

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

HTH

/抢

© www.soinside.com 2019 - 2024. All rights reserved.