Here is my Transaction
class:
class Transaction(object):
def __init__(self, company, num, price, date, is_buy):
self.company = company
self.num = num
self.price = price
self.date = datetime.strptime(date, "%Y-%m-%d")
self.is_buy = is_buy
And when I’m trying to run the date
function:
tr = Transaction('AAPL', 600, '2013-10-25')
print tr.date
I’m getting the following error:
self.date = datetime.strptime(self.d, "%Y-%m-%d")
AttributeError: 'module' object has no attribute 'strptime'
How can I fix that?
Aran-Fey
38.9k11 gold badges103 silver badges148 bronze badges
asked Oct 20, 2013 at 16:45
2
If I had to guess, you did this:
import datetime
at the top of your code. This means that you have to do this:
datetime.datetime.strptime(date, "%Y-%m-%d")
to access the strptime
method. Or, you could change the import statement to this:
from datetime import datetime
and access it as you are.
The people who made the datetime
module also named their class datetime
:
#module class method
datetime.datetime.strptime(date, "%Y-%m-%d")
answered Oct 20, 2013 at 16:46
4
Use the correct call: strptime
is a classmethod of the datetime.datetime
class, it’s not a function in the datetime
module.
self.date = datetime.datetime.strptime(self.d, "%Y-%m-%d")
As mentioned by Jon Clements in the comments, some people do from datetime import datetime
, which would bind the datetime
name to the datetime
class, and make your initial code work.
To identify which case you’re facing (in the future), look at your import statements
import datetime
: that’s the module (that’s what you have right now).from datetime import datetime
: that’s the class.
answered Oct 20, 2013 at 16:46
Thomas OrozcoThomas Orozco
52.8k10 gold badges111 silver badges116 bronze badges
1
I got the same problem and it is not the solution that you told. So I changed the «from datetime import datetime» to «import datetime». After that with
the help of «datetime.datetime» I can get the whole modules correctly. I guess this is the correct answer to that question.
answered Mar 14, 2020 at 22:42
KursadKursad
1131 silver badge6 bronze badges
Values may differ depending on usage.
import datetime
date = datetime.datetime.now()
date.strftime('%Y-%m-%d') # date variable type is datetime
The value of the date variable must be a string::
date = '2021-09-06'
datetime.datetime.strptime(date, "%Y-%m-%d")
str(datetime.datetime.strptime(date, "%Y-%m-%d")) # show differently
answered Sep 6, 2021 at 6:34
AyseAyse
5674 silver badges11 bronze badges
The solutions mentioned by the others are correct. But for me, it was a problem with another library importing datetime module for me and overriding the datetime class I was importing.
an example with tsai library:
from datetime import datetime
from tsai.all import *
This will give you the error: 'module' object has no attribute 'strptime'
.
In this case, just flip the order of imports or just don’t import everything (even if the documentation does that) :
from tsai.all import *
from datetime import datetime
answered Dec 19, 2022 at 15:25
bibs2091bibs2091
211 silver badge4 bronze badges
Table of Contents
Hide
- What is AttributeError: ‘module’ object has no attribute ‘strptime’
- How to resolve AttributeError: ‘module’ object has no attribute ‘strptime’
- Solution 1: Import the datetime module directly and access the method through its class name
- Approach 2 – Import the datetime class from the datetime module
- Conclusion
The AttributeError: ‘module’ object has no attribute ‘strptime’ occurs if you have imported the datetime
module and directly if we are using the datetime.strptime()
method on the datetime
module.
The datetime
is a module, and it does not have the strptime()
method; instead, we need to use the datetime
class name, which has the method correct method and the syntax for the same is datetime.datetime.strptime()
In this tutorial, we will look into what exactly is AttributeError: ‘module’ object has no attribute ‘strptime’ and how to resolve the error with examples.
First, let us see how to reproduce this issue and why developers face this particular issue with a simple example.
# import datetime module
import datetime
start_date = "2022-05-06"
# convert into datetime object and print
print(datetime.strptime(start_date, "%Y-%m-%d"))
Output
Traceback (most recent call last):
File "c:PersonalIJSCodeCode.py", line 7, in <module>
print(datetime.strptime(start_date, "%Y-%m-%d"))
AttributeError: module 'datetime' has no attribute 'strptime'
In the above example, we are importing the datetime module and trying to convert the string datetime to a datetime object using the datetime.strptime()
method.
When we run the code, we get an AttributeError: module ‘datetime’ has no attribute ‘strptime’
The issue occurs because the datetime
module does not have a strptime()
method, and hence it is throwing an error.
The datetime
module has a class name called datetime
which in turn has the method strptime()
.
Since the module name and class name are also the same, it leads to a lot of confusion for the new developers, and they feel it’s ambiguous to use datetime multiple times.
How to resolve AttributeError: ‘module’ object has no attribute ‘strptime’
We can resolve the ‘module’ object has no attribute ‘strptime’ by using the strptime()
method, which is present inside the datetime
class.
There are two ways to access the strptime()
method correctly.
Solution 1: Import the datetime module directly and access the method through its class name
If you are importing the datetime module directly, then the best way to resolve the error is to use datetime.datetime.strptime()
method.
Syntax
datetime.datetime.strptime()
Here the first datetime
is a module and the second datetime
is a class which has a method strptime()
Example –
# import datetime module
import datetime
start_date = "2022-05-06"
# convert into datetime object and print
print(datetime.datetime.strptime(start_date, "%Y-%m-%d"))
Output
2022-05-06 00:00:00
Approach 2 – Import the datetime class from the datetime module
Another way to resolve the issue is to import the datetime class directly using the syntax from datetime import datetime
Syntax
from datetime import datetime
While using the from syntax, we import the datetime
class directly and using the class name; we can access all of its methods. We do not have to prefix/use the module name here.
Example –
# import datetime class from datetime module
from datetime import datetime
start_date = "2022-05-06"
# convert into datetime object and print
print(datetime.strptime(start_date, "%Y-%m-%d"))
Output
2022-05-06 00:00:00
Conclusion
The datetime
module does not have the strptime()
method; hence if we try to use datetime.strptime()
directly we get AttributeError: ‘module’ object has no attribute ‘strptime’
We can resolve the issue using the datetime class name instead of the datetime module. An alternate way is to import the datetime class using the from keyword directly.
Srinivas Ramakrishna is a Solution Architect and has 14+ Years of Experience in the Software Industry. He has published many articles on Medium, Hackernoon, dev.to and solved many problems in StackOverflow. He has core expertise in various technologies such as Microsoft .NET Core, Python, Node.JS, JavaScript, Cloud (Azure), RDBMS (MSSQL), React, Powershell, etc.
Sign Up for Our Newsletters
Subscribe to get notified of the latest articles. We will never spam you. Be a part of our ever-growing community.
By checking this box, you confirm that you have read and are agreeing to our terms of use regarding the storage of the data submitted through this form.
Attributeerror: module datetime has no attribute strptime error occurs because strptime is not directly available in datetime package. Actually, datetime has a class by the name of datetime inside the same. If we make an improper invoking statement, we get the same error. In this article, we will understand this error with a practical examples. We will also see the easiest way to fix this issue. So let’s begin.
In order to understand this error, we will firstly see a code sample and run the same. Then on the basis of that, we will understand the root cause.
import datetime
date_var = '2022-06-19'
datetime.strptime(date_var, "%Y-%m-%d")
when we run the code we get the above error.
Srinivas Ramakrishna is a Solution Architect and has 14+ Years of Experience in the Software Industry. He has published many articles on Medium, Hackernoon, dev.to and solved many problems in StackOverflow. He has core expertise in various technologies such as Microsoft .NET Core, Python, Node.JS, JavaScript, Cloud (Azure), RDBMS (MSSQL), React, Powershell, etc.
Sign Up for Our Newsletters
Subscribe to get notified of the latest articles. We will never spam you. Be a part of our ever-growing community.
By checking this box, you confirm that you have read and are agreeing to our terms of use regarding the storage of the data submitted through this form.