datetime
is a module which contains a function1 that is also called datetime
.
You can use it like this:
import datetime
# ^^^^^^^^
# the module
x = datetime.datetime(...)
# ^^^^^^^^ the function
# ^^^^^^^^ the module
Here datetime
refers to the module, and datetime.datetime
refers to the function in the module.
Or you can only import the function from the module, like this:
from datetime import datetime
# ^^^^^^^^ ^^^^^^^^
# the module the function
x = datetime(...)
# ^^^^^^^^ the function
You used a mixture of those two, which does not work:
from datetime import datetime
# ^^^^^^^^ ^^^^^^^^
# the module the function
x = datetime.datetime(...)
# ^^^^^^^^ this does not exist
# ^^^^^^^^ the function
In case use used
import datetime
from datetime import datetime
then after the first line, datetime
would refer to the module (which on its own would have been correct), but the second line would overwrite that and datetime
would no longer refer to the module, but to the function.
1 Actually, datetime.datetime
is a class, not a function, but for the purpose of this answer it does not matter.