Borislav Hadzhiev
Wed Apr 20 2022·2 min read
Photo by Chris Altamirano
The error "AttributeError module 'datetime' has no attribute 'fromtimestamp'"
occurs when we call the fromtimestamp
method on the datetime
module. To
solve the error, use the following import import datetime
and call the method
as datetime.datetime.fromtimestamp(...)
.
Here is an example of how the error occurs.
import time import datetime # ⛔️ AttributeError: module 'datetime' has no attribute 'fromtimestamp' today = datetime.fromtimestamp(time.time()) print(today)
The issue in the code sample is that we are trying to call the fromtimestamp
method directly on the datetime
module.
To solve the error, call the fromtimestamp
method on the datetime
class
instead.
import time import datetime today = datetime.datetime.fromtimestamp(time.time()) # 👇️ 2022-05-14 13:16:59.426594 print(today)
datetime.py
as that would shadow the official datetime
module.Alternatively, you can change your import statement to import the datetime
class from the datetime
module to not have to type datetime.datetime
which
looks quite confusing.
import time from datetime import datetime today = datetime.fromtimestamp(time.time()) # 👇️ 2022-05-14 13:16:59.426594 print(today)
We imported the datetime class from the datetime module and called the fromtimestamp method on the class.
It is quite confusing that there is a class named datetime
in the datetime
module.
You can make your code a bit easier to read by using an alias in your import statement.
import time from datetime import datetime as dt today = dt.fromtimestamp(time.time()) # 👇️ 2022-05-14 13:16:59.426594 print(today)
We aliased the datetime
class to dt
, so you would call the fromtimestamp
method as dt.fromtimestamp()
instead of datetime.fromtimestamp()
.
You can also pass a tz
keyword argument to the method, to convert the
timestamp to the specific time zone.
import time from datetime import datetime as dt, timezone today = dt.fromtimestamp(time.time(), tz=timezone.utc) # 👇️ 2022-05-14 10:21:52.540533+00:00 print(today)
The best way to start debugging the "AttributeError module 'datetime' has no
attribute 'fromtimestamp'" is to call the dir()
function passing it the
imported module.
import datetime """ [ 'MAXYEAR', 'MINYEAR', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'date', 'datetime', 'datetime_CAPI', 'sys', 'time', 'timedelta', 'timezone', 'tzinfo' ] """ print(dir(datetime))
If you pass a module object to the dir() function, it returns a list of names of the module's attributes.
We can see that the datetime
module has no attribute named fromtimestamp
, so
it must be an attribute in one of the module's classes.
We can also see that the datetime
module has an attribute datetime
, which is
what we used to successfully call the fromtimestamp()
method.
If you import the datetime
class from the datetime
module and pass it to the
dir()
function, you will see the fromtimestamp
method in the list of
attributes.
from datetime import datetime # 👇️ [... 'fromtimestamp', ...] print(dir(datetime))
If you got the error "AttributeError: partially initialized module 'datetime'
has no attribute 'fromtimestamp' (most likely due to a circular import)", there
is a file called datetime.py
in your local codebase.
To solve the error, make sure to not use names of built-in or remote modules,
e.g. datetime.py
, for your local files.