datetime
is a module which contains a type that is also called datetime
. You appear to want to use both, but you're trying to use the same name to refer to both. The type and the module are two different things and you can't refer to both of them with the name datetime
in your program.
If you need to use anything from the module besides the datetime
type (as you apparently do), then you need to import the module with import datetime
. You can then refer to the "date" type as datetime.date
and the datetime type as datetime.datetime
.
You could also do this:
from datetime import datetime, date
today_date = date.today()
date_time = datetime.strp(date_time_string, '%Y-%m-%d %H:%M')
Here you import only the names you need (the datetime and date types) and import them directly so you don't need to refer to the module itself at all.
Ultimately you have to decide what names from the module you need to use, and how best to use them. If you are only using one or two things from the module (e.g., just the date
and datetime
types), it may be okay to import those names directly. If you're using many things, it's probably better to import the module and access the things inside it using dot syntax, to avoid cluttering your global namespace with date-specific names.
Note also that, if you do import the module name itself, you can shorten the name to ease typing:
import datetime as dt
today_date = dt.date.today()
date_time = dt.datetime.strp(date_time_string, '%Y-%m-%d %H:%M')