AttributeError: module 'datetime' has no attribute 'strptime'
mean and how can you easily fix it?
When parsing a string and transforming that to a date you can make use of the function
To use this function simply
as you normally would with any module, and use the function as follows:
>>> import datetime >>> datetime.datetime.strptime("16/06/22", "%d/%m/%y") datetime.datetime(2022, 6, 16, 0, 0)
Notice in the above code that the function
is found within the module
, therefore, if your code just has
at the top you will need to enter the module and class followed by the function.
A workaround is to import the module and class in your import declaration, like so:
>>> from datetime import datetime >>> datetime.strptime("17/08/22", "%d/%m/%y") datetime.datetime(2022, 8, 17, 0, 0)
Notice in this code that the
only has the direct class
to reference as the import declaration on pulled that class from the module.
Another popular mistake with this approach is thinking you can import
from the import declaration, like so:
>>> from datetime import strptime ImportError: cannot import name 'strptime' from 'datetime'
As you can see from this error you cannot
as it’s a function in the
class found in the
If you’re experiencing issues with the
method check your import declaration for
. Have you imported the module and class or just the module? If you’ve imported the module and class using
from datetime import datetime
then your function call will be
, otherwise if you’ve used
you would need to reference the module and class along with the function, like so: