Valerie Meunier

Valerie Meunier

  • NA
  • 693
  • 82.4k

import datetime - from datetime import datetime question

Mar 19 2023 10:09 PM

Hi

I have several case with different results:

Could someone explain me why it gives an error or not. It seems that the order of the import plays a role.

Thanks

V

import datetime
from datetime import datetime
dt=datetime.datetime.strptime(input("Input date (dd-mm-jjjj): "),"%d-%m-%Y")
=> AttributeError: type object 'datetime.datetime' has no attribute 'datetime'

same as above but different order:
from datetime import datetime
import datetime
dt=datetime.datetime.strptime(input("Input date (dd-mm-jjjj): "),"%d-%m-%Y")
=> no error

from datetime import datetime
import datetime
dt=datetime.strptime(input("Input date (dd-mm-jjjj): "),"%d-%m-%Y")
=> AttributeError: type object 'datetime.datetime' has no attribute 'strptime'

import datetime
from datetime import datetime
dt=datetime.strptime(input("Input date (dd-mm-jjjj): "),"%d-%m-%Y")
=> no error


Answers (2)