How to Solve Python AttributeError: type object ‘datetime.datetime’ has no attribute ‘fromisoformat’

by | Programming, Python, Tips

This error occurs when you try to use datetime.fromisoformat with a Python version 3.6 or older. The datetime.fromisoformat method is not present in Python version 3.6 and older. You can solve this error by upgrading your Python version to 3.7 or newer. Alternatively, if you want to keep the Python version, you can import backports-datetime-fromisoformat using:

pip install backports-datetime-fromisoformat

This tutorial will go through the error and how to solve it with code examples.


AttributeError: type object ‘datetime’ has no attribute ‘fromisoformat’

AttributeError occurs in a Python program when we try to access an attribute (method or property) that does not exist for a particular object. The fromisoformat method is only an attribute of the datetime class in Python versions 3.7 and newer. The datetime.fromisoformat method allows us to create a datetime object from an isoformat string that can include a date and a time. Whereas date.fromisoformat allows us to create a date object from an isoformat string containing only a date.

Example

Consider the following example where we want to convert a string containing a date-time in ISO format to a datetime object. ISO format means the string is in yyyy-mm-dd.

from datetime import datetime

date = datetime.fromisoformat("2016-06-06T16:21:54")

print(date)

Note, that we include T to indicate the time component of the isoformat string. Let’s run the code to see what happens:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-3-8edda85c99e0> in <module>
      1 from datetime import datetime
      2 
----> 3 date = datetime.fromisoformat("2016-06-06T16:21:54")
      4 
      5 print(date)

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

The error occurs because we are using Python 3.6. We can verify this by importing sys and then printing sys.version.

import sys

print(sys.version)
3.6.13 |Anaconda, Inc.| (default, Feb 23 2021, 12:58:59) 
[GCC Clang 10.0.0 ]

The datetime.fromisoformat method was included in Python 3.7. Therefore any versions older than 3.7 do not have this method.

Solution #1: Upgrade to Python 3.7+

The first way we can solve this error is by upgrading to the latest version of Python. Suppose we are in a conda environment with Python 3.6 installed. We can upgrade to the newest version of Python with the following command:

conda update python

We can then check we are using the newest version of Python with the sys module.

import sys

print(sys.version)
3.9.12 (main, Apr  5 2022, 01:53:17) 
[Clang 12.0.0 ]

With the latest version of Python being newer than 3.6, we can use the datetime.fromisoformat method as follows:

from datetime import datetime

date = datetime.fromisoformat("2016-06-06T16:21:54")

print(date)
2016-06-06 16:21:54

We successfully created a datetime object from an ISO formatted string.

Solution #2: Use backports-datetime-fromisoformat

The second way to solve the error is to install the module backports-datetime-fromisoformat. The module provides a backport of Python 3.7’s datetime.fromisoformat method to earlier versions of Python 3. This solution is helpful if we want to keep the Python 3.6 version or older.

We can install the module from the command line as follows:

pip install backports-datetime-fromisoformat

We can use the datetime.fromisoformat method as follows:

from datetime import datetime
from backports.datetime_fromisoformat import MonkeyPatch
MonkeyPatch.patch_fromisoformat()

date = datetime.fromisoformat("2016-06-06T16:21:54+10:00")

print(date)

We successfully created a datetime object from an ISO formatted string.

Summary

Congratulations on reading to the end of this tutorial!

For further reading on AttributeErrors involving datetime, go to the articles:

To learn more about Python for data science and machine learning, go to the online courses page on Python for the most comprehensive courses available.

Have fun and happy researching!

Profile Picture
Senior Advisor, Data Science | [email protected] | + posts

Suf is a senior advisor in data science with deep expertise in Natural Language Processing, Complex Networks, and Anomaly Detection. Formerly a postdoctoral research fellow, he applied advanced physics techniques to tackle real-world, data-heavy industry challenges. Before that, he was a particle physicist at the ATLAS Experiment of the Large Hadron Collider. Now, he’s focused on bringing more fun and curiosity to the world of science and research online.

Buy Me a Coffee ✨