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

Python version 3.6 and older don't have the fromisoformat() methods - as mentioned in other documentation - both datetime.fromisoformat (docs) and date.fromisoformat (docs) are not available.

You can use this code I wrote to implement this in Python 3.6. I prefer not to install additional dependencies for functions I hardly use - in my case, I only use it in a test.

Python3.6 and below

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

You should refactor datetime.fromisoformat('2021-08-12') to use datetime.strptime like this:

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)

The issue here is actually that fromisoformat is not available in Python versions older than 3.7, you can see that clearly stated in the documenation here.

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.

I had the same issue and found this:

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)

Works like a charm.