AttributeError module 'datetime' has no attribute 'strptime'

avatar
Borislav Hadzhiev

Last updated: Apr 8, 2024
11 min

banner

# Table of Contents

  1. Module 'datetime' has no attribute 'strptime'
  2. Module 'datetime' has no attribute 'strftime'
  3. Module 'datetime' has no attribute 'today'
  4. Module 'datetime' has no attribute 'fromtimestamp'
  5. Module 'datetime' has no attribute 'now'

# AttributeError module 'datetime' has no attribute 'strptime'

The error "AttributeError module 'datetime' has no attribute 'strptime'" occurs when we try to call the strptime method directly on the datetime module.

To solve the error, use the following import import datetime and call the strptime method as datetime.datetime.strptime(...).

attributeerror module datetime has no attribute strptime

Here is an example of how the error occurs.

main.py
import datetime # ⛔️ AttributeError: module 'datetime' has no attribute 'strptime' dt = datetime.strptime("21/11/22 09:30", "%d/%m/%y %H:%M") print(dt)
If you got the error "AttributeError module 'datetime' has no attribute strftime", scroll down to the next subheading.

The issue is that we are trying to call the strptime method directly on the datetime module.

# Call the strptime method on the datetime class instead

To solve the error, call the strptime method on the datetime class instead.

main.py
import datetime dt = datetime.datetime.strptime( "21/11/22 09:30", "%d/%m/%y %H:%M" ) print(dt) # πŸ‘‰οΈ 2022-11-21 09:30:00

call strptime method on datetime class

Make sure you haven't named your local module datetime.py as that would shadow the official datetime module.

# Alternatively, you can import the datetime class directly

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.

main.py
from datetime import datetime dt = datetime.strptime("21/11/22 09:30", "%d/%m/%y %H:%M") print(dt) # πŸ‘‰οΈ 2022-11-21 09:30:00

import datetime class directly

We imported the datetime class from the datetime module and called the strptime() method on the class.

It is quite confusing that there is a class named datetime in the datetime module.

The following import statement imports the datetime class.

main.py
from datetime import datetime # πŸ‘‡οΈ <class 'datetime.datetime'> print(datetime)

The following import statement imports the entire datetime module.

main.py
import datetime # πŸ‘‡οΈ <module 'datetime' from '/home/borislav/anaconda3/lib/python3.9/datetime.py'> print(datetime) # πŸ‘‡οΈ <class 'datetime.datetime'> print(datetime.datetime)

# Using aliases to make your code more readable

You can make your code a bit easier to read by using an alias in your import statement.

main.py
# πŸ‘‡οΈ alias datetime class to dt from datetime import datetime as dt result = dt.strptime("21/11/22 09:30", "%d/%m/%y %H:%M") print(result) # πŸ‘‰οΈ 2022-11-21 09:30:00

using aliases to make your code more readable

We aliased the datetime class to dt, so you would call the strptime method as dt.strptime() instead of datetime.strptime().

# Checking what attributes an object has

The best way to start debugging is to call the dir() function passing it the imported module.

main.py
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.

If you try to access any attribute that is not in this list, you will get the "AttributeError: module has no attribute".

We can see that the datetime module has no attribute named strptime, 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 strptime() method.

If you import the datetime class from the datetime module and pass it to the dir() function, you will see the strptime method in the list of attributes.

main.py
from datetime import datetime # πŸ‘‡οΈ [... 'strptime', ...] print(dir(datetime))

If you got the error "AttributeError: partially initialized module 'datetime' has no attribute 'strptime' (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 the names of built-in or remote modules, e.g. datetime.py, for your local files.

# Table of Contents

  1. Module 'datetime' has no attribute 'strftime'
  2. Module 'datetime' has no attribute 'today'
  3. Module 'datetime' has no attribute 'fromtimestamp'
  4. Module 'datetime' has no attribute 'now'

# AttributeError module 'datetime' has no attribute 'strftime'

The error "AttributeError module 'datetime' has no attribute 'strftime'" occurs when we try to call the strftime method directly on the datetime module.

To solve the error, create a datetime object and call the strftime method on the object instead.

attributeerror module datetime has no attribute strftime

Here is an example of how the error occurs.

main.py
import datetime # ⛔️ AttributeError: module 'datetime' has no attribute 'strftime' print(datetime.strftime("%d/%m/%y"))

The issue in the code sample is that we are trying to call the strftime method directly on the datetime module.

To solve the error, we have to create a datetime object first and call the strftime method on the object.

main.py
import datetime d = datetime.datetime(2022, 11, 24, 9, 30, 0) # πŸ‘‡οΈ 24/11/22 print(d.strftime("%d/%m/%y")) # πŸ‘‡οΈ ️Thursday, 24. November 2022 09:30AM print(d.strftime("%A, %d. %B %Y %I:%M%p")) # πŸ‘‡οΈ Saturday, 14. May 2022 11:40AM d2 = datetime.datetime.now() # πŸ‘‰οΈ current date print(d2.strftime("%A, %d. %B %Y %I:%M%p"))
Make sure you haven't named your local module datetime.py as that would shadow the official datetime module.

We imported the datetime module and instantiated the datetime class.

The datetime class has a strftime method which we can use to get a string representation of the date and time, controlled by an explicit format string.

It is quite confusing that the datetime module has a datetime class.

An alternative to chaining datetime.datetime, is to import the class from the module.

main.py
from datetime import datetime d = datetime(2022, 11, 24, 9, 30, 0) # πŸ‘‡οΈ 24/11/22 print(d.strftime("%d/%m/%y")) # πŸ‘‡οΈ ️Thursday, 24. November 2022 09:30AM print(d.strftime("%A, %d. %B %Y %I:%M%p")) # πŸ‘‡οΈ Saturday, 14. May 2022 11:40AM d2 = datetime.now() print(d2.strftime("%A, %d. %B %Y %I:%M%p"))

We imported the datetime class from the datetime module, so we don't have to use datetime.datetime when instantiating the class.

You can also use an alias in your import statement.

main.py
from datetime import datetime as dt d = dt(2022, 11, 24, 9, 30, 0) # πŸ‘‡οΈ 24/11/22 print(d.strftime("%d/%m/%y")) # πŸ‘‡οΈ ️Thursday, 24. November 2022 09:30AM print(d.strftime("%A, %d. %B %Y %I:%M%p")) # πŸ‘‡οΈ Saturday, 14. May 2022 11:40AM d2 = dt.now() print(d2.strftime("%A, %d. %B %Y %I:%M%p"))

We aliased the datetime class to dt, so we can access methods on the class as dt.now() instead of datetime.now().

The best way to start debugging is to call the dir() function passing it the imported module.

main.py
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.

If you try to access any attribute that is not in this list, you will get the "AttributeError: module has no attribute".

We can see that the datetime module has no attribute named strftime, so it must be an attribute in one of the module's classes.

However, if we pass the datetime object to the dir() function, we can see that it has an attribute strftime.

main.py
import datetime d = datetime.datetime(2022, 11, 24, 9, 30, 0) # [... 'strftime' ...] print(dir(d))

The dir() function is very useful when you need to look at what exactly you are importing and which attributes are available on the imported module.

# Table of Contents

  1. Module 'datetime' has no attribute 'today'
  2. Module 'datetime' has no attribute 'fromtimestamp'
  3. Module 'datetime' has no attribute 'now'

# AttributeError module 'datetime' has no attribute 'today'

The error "AttributeError module 'datetime' has no attribute 'today'" occurs when we try to call the today method directly on the datetime module.

To solve the error, use the following import import datetime and call the today method as datetime.date.today().

attributeerror module datetime has no attribute today

Here is an example of how the error occurs.

main.py
import datetime # ⛔️ AttributeError: module 'datetime' has no attribute 'today' print(datetime.today())

The issue in the code sample is that we are trying to call the today method directly on the datetime module.

To solve the error, call the today method on the date class instead.

main.py
# πŸ‘‡οΈ 2022-05-14 print(datetime.date.today()) # πŸ‘‡οΈ 2022-05-14 13:45:57.409176 print(datetime.datetime.today())

When called on a date object, the today method returns the current local date.

When called on a datetime object, the today method returns the local datetime.

Make sure you haven't named your local module 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.

main.py
from datetime import datetime, date # πŸ‘‡οΈ 2022-05-14 print(date.today()) # πŸ‘‡οΈ 2022-05-14 13:45:57.409176 print(datetime.today())

We imported the datetime and date classes from the datetime module and called the today() method on the classes.

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.

main.py
from datetime import datetime as dt, date # πŸ‘‡οΈ 2022-05-14 print(date.today()) # πŸ‘‡οΈ 2022-05-14 13:45:57.409176 print(dt.today())

We aliased the datetime class to dt, so you would call the today method as dt.today() instead of datetime.today().

The best way to start debugging is to call the dir() function passing it the imported module.

main.py
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.

If you try to access any attribute that is not in this list, you will get the "AttributeError: module has no attribute".

We can see that the datetime module has no attribute named today, 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 today() method.

If you import the datetime class from the datetime module and pass it to the dir() function, you will see the today method in the list of attributes.

main.py
from datetime import datetime, date # πŸ‘‡οΈ [... 'today' ...] print(dir(datetime)) # πŸ‘‡οΈ [... 'today' ...] print(dir(date))

If you got the error "AttributeError: partially initialized module 'datetime' has no attribute 'today' (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 the names of built-in or remote modules, e.g. datetime.py, for your local files.

# Table of Contents

  1. Module 'datetime' has no attribute 'fromtimestamp'
  2. Module 'datetime' has no attribute 'now'

# AttributeError module 'datetime' has no attribute 'fromtimestamp'

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

attributeerror module datetime has no attribute fromtimestamp

Here is an example of how the error occurs.

main.py
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.

main.py
import time import datetime today = datetime.datetime.fromtimestamp(time.time()) # πŸ‘‡οΈ 2022-05-14 13:16:59.426594 print(today)
Make sure you haven't named your local module 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.

main.py
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.

main.py
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.

main.py
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 is to call the dir() function passing it the imported module.

main.py
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.

If you try to access any attribute that is not in this list, you will get the "AttributeError: module has no attribute".

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.

main.py
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 the names of built-in or remote modules, e.g. datetime.py, for your local files.

# AttributeError module 'datetime' has no attribute 'now'

The error "AttributeError module 'datetime' has no attribute 'now'" occurs when we try to call the now method directly on the datetime module.

To solve the error, use the following import import datetime and call the now method as datetime.datetime.now().

attributeerror module datetime has no attribute now

Here is an example of how the error occurs.

main.py
import datetime # ⛔️ AttributeError: module 'datetime' has no attribute 'now' print(datetime.now())

The issue in the code sample is that we are trying to call the now method directly on the datetime module.

To solve the error, call the now method on the datetime class instead.

main.py
import datetime # πŸ‘‡οΈ 2022-05-14 12:48:41.270852 print(datetime.datetime.now())
Make sure you haven't named your local module 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.

main.py
from datetime import datetime # πŸ‘‡οΈ 2022-05-14 12:48:41.270852 print(datetime.now())

We imported the datetime class from the datetime module and called the now method on the class.

It is quite confusing that there is a class named datetime in the datetime module.

You can get around this by using an alias in your import statement.

main.py
# πŸ‘‡οΈ alias datetime class to dt from datetime import datetime as dt # πŸ‘‡οΈ 2022-05-14 12:48:41.270852 print(dt.now())

We aliased the datetime class to dt, so you would call the now method as dt.now() instead of datetime.now().

The best way to start debugging is to call the dir() function passing it the imported module.

main.py
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.

If you try to access any attribute that is not in this list, you will get the "AttributeError: module has no attribute".

We can see that the datetime module has no attribute named now, 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 now() method.

If you import the datetime class from the datetime module and pass it to the dir() function, you will see the now method in the list of attributes.

main.py
from datetime import datetime # πŸ‘‡οΈ [... 'now', ...] print(dir(datetime))

If you got the error "AttributeError: partially initialized module 'datetime' has no attribute 'now' (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 the names of built-in or remote modules, e.g. datetime.py, for your local files.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.