I have a script that needs to execute the following at different lines in the script:
today_date = datetime.date.today() date_time = datetime.strp(date_time_string, '%Y-%m-%d %H:%M')
In my import
statements I have the following:
from datetime import datetime import datetime
I get the following error:
AttributeError: 'module' object has no attribute 'strp'
If I change the order of the import
statements to:
import datetime from datetime import datetime
I get the following error:
AttributeError: 'method_descriptor' object has no attribute 'today'
If I again change the import
statement to:
import datetime
I get the following error:
AttributeError: 'module' object has no attribute 'strp'
What is going on here and how do I get both to work?
datetime refers to the datetime class within the datetime module. If you do 'from datetime import datetime', you're only importing the datetime class, so when you refer to 'datetime' in your code, it's referring to the datetime class, not the whole datetime module.
datetime is a built-in Python module. Developers don't need to install it separately. It's right there. With the datetime module, you get to work with different classes that work perfectly with date and time.
In Python, date and time are not a data type of their own, but a module named datetime can be imported to work with the date as well as time. Python Datetime module comes built into Python, so there is no need to install it externally. Python Datetime module supplies classes to work with date and time.
The time module is principally for working with Unix time stamps; expressed as a floating point number taken to be seconds since the Unix epoch. the datetime module can support many of the same operations, but provides a more object oriented set of types, and also has some limited support for time zones.
Your trouble is that you have some code that is expecting datetime
to be a reference to the datetime
module and other code that is expecting datetime
to be a reference to the datetime
class. Obviously, it can't be both.
When you do:
from datetime import datetime import datetime
You are first setting datetime
to be a reference to the class, then immediately setting it to be a reference to the module. When you do it the other way around, it's the same thing, but it ends up being a reference to the class.
You need to rename one of these references. For example:
import datetime as dt from datetime import datetime
Then you can change references in the form datetime.xxxx
that refer to the module to dt.xxxx
.
Or else just import datetime
and change all references to use the module name. In other words, if something just says datetime(...)
you need to change that reference to datetime.datetime
.
Python has a fair bit of this kind of thing in its library, unfortunately. If they followed their own naming guidelines in PEP 8, the datetime
class would be named Datetime
and there'd be no problem using both datetime
to mean the module and Datetime
to mean the class.
You cannot use both statements; the datetime
module contains a datetime
type. The local name datetime
in your own module can only refer to one or the other.
Use only import datetime
, then make sure that you always use datetime.datetime
to refer to the contained type:
import datetime today_date = datetime.date.today() date_time = datetime.datetime.strptime(date_time_string, '%Y-%m-%d %H:%M')
Now datetime
is the module, and you refer to the contained types via that.
Alternatively, import all types you need from the module:
from datetime import date, datetime today_date = date.today() date_time = datetime.strptime(date_time_string, '%Y-%m-%d %H:%M')
Here datetime
is the type from the module. date
is another type, from the same module.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With