type object 'datetime.datetime' has no attribute 'fromisoformat'
Asked Answered
D

5

43

I have a script with the following import:

from datetime import datetime

and a piece of code where I call:

datetime.fromisoformat(duedate)

Sadly, when I run the script with an instance of Python 3.6, the console returns the following error:

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

I tried to run it from two instances of anaconda (3.7 and 3.8) and it works nice and smooth. I supposed there was an import problem so I tried to copy datetime.py from anaconda/Lib to the script directory, with no success.

The datetime.py clearly contains the class datetime and the method fromisoformat but still it seems unlinked. I even tried to explicitly link the datetime.py file, with the same error:

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

Can you help me? My ideas are over...

Dissipation answered 17/2, 2020 at 16:22 Comment(0)
D
58

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.
Draughts answered 17/2, 2020 at 16:30 Comment(3)
doh... do you know an alternative for older python 3's instances?Dissipation
perhaps this is what you are looking forDraughts
isoformat() seems to have the opposite effect since it translates to ISO8601 format. I'll look for alternative solutions. Thanks btwDissipation
J
16

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.

Jimjams answered 25/3, 2020 at 15:35 Comment(2)
Works great, but is there a function like timedelta in datetime.datetime. How can you increment the date here ?Rosary
It works just like you would expect. Since you still import datetime and just update the fromisoformat the rest stays unchanged.Jimjams
I
12

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)
Immutable answered 26/8, 2021 at 19:15 Comment(0)
P
4

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
Phosphorescence answered 18/2, 2022 at 15:35 Comment(0)
B
2

I ran into this problem, or the same error message, anyway, even though I am running Python 3.11.6. In my case, the real issue is confusion over the import name and the class name. To wit:

(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

/rob

Bustard answered 26/11, 2023 at 3:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.