Module datetime has no attribute strptime

How to fix AttributeError: Module datetime has no attribute strptime?

Sometimes, while working with the datetime module, coders face error message Module datetime has no attribute strptime. This error mostly occurs when you try to call the strptime() method directly from the datetime module. The strptime() method is actually defined in the datetime class, so you need to call it from a datetime object. For example, the following code will cause the error as shown in the image:

import datetime
datetime.strptime('2023-08-17', '%Y-%m-%d')

This code will display the following error.

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

So, In this article, I will discuss the possible reasons and solutions of the error and will also give some tips and tricks to avoid this error in the future. Let’s get started!

Why this Error?

There are a few possible causes of this error:

  • You have not imported the datetime module.
  • You are trying to call the strptime() method without creating a datetime object.
  • You are using an incorrect format string for the strptime() method.
  • You have created some typo mistakes and trying to import datetime module using the incorrect spellings.

Moreover, following are some incorrect methods to import datetime module which also leads towards an error. For example, if you will import the datetime module as follows then you will face error.

 datetime.strptime('2023-08-17', '%Y-%m-%d')

This code will display following error.

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

Also make sure you are importing the strptime() method in correct way. Following is the incorrect method to import the strptime() method. This will lead towards as error message as shown in the image.

import datetime

This code will display following error message.

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

How to solve error “Module datetime has no attribute strptime”

To fix the error, you need to make sure that you have imported the datetime module and you are calling the strptime() method from a datetime object. You also need to make sure that you are using the correct format string for the strptime() method. Following are some examples of how to fix the error:

Solution 1: Correct way to import the datetime module

Check you are importing the datetime module as shown in the following image.

import datetime

Solution 2: Correct way to call the strptime() method

The following method is correct to call the strptime() method. Check carefully you are following the syntax and run code as shown in the following image.

dt = datetime.datetime.strptime('2023-08-17', '%Y-%m-%d')

Here is a breakdown of the code:

  • The first line imports the datetime module.
  • The second line creates a datetime object, calls the strptime() method and passes the string ‘2023-08-17’ as the first argument and the format string ‘%Y-%m-%d’ as the second argument.

Solution 3:Check for Typos and Case Sensitivity

Make sure that you are using the correct case for datetime and strptime. As Python is case-sensitive, so datetime and strptime should be lowercase.


Q1: I’m using a virtual environment, could I face this issue?

Ans: It’s possible that there might be an issue with your virtual environment’s configuration. Ensure that you’re using the correct Python interpreter within your virtual environment and that there are no conflicting libraries or modules.

Q2: Can this error occur in all versions of Python?

Ans: No, this error is typically encountered in Python versions prior to 3. If you are using Python 2, for example, you might not have access to the strptime method directly within the datetime module. It’s recommended to use Python 3 or a newer version where this method is available.

Q3: Can I use the strptime method with date formats other than year-month-day?

Ans: Yes, the strptime method can be used with various date and time formats. You need to construct the format_string based on the format of the date in your date_string. You may check the official documentation for other formats.


I hope this article helps you understand the error message “Module datetime has no attribute strptime” and how to fix it. You can also check latest release and updates related to datetime object on official documentation. If you have any other questions, please let me know.