Valerie Meunier

Valerie Meunier

  • 1.5k
  • 686
  • 48.6k

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)