0

What is the difference between: from datetime import datetime and import datetime

I often just do from datetime import datetime, but what is the difference, and why do some people use one over the other?

Language:Python3.7

bigshirtjp
  • 43
  • 6
  • 1
    Does this answer your question? [python "import datetime" v.s. "from datetime import datetime"](https://stackoverflow.com/questions/15707532/python-import-datetime-v-s-from-datetime-import-datetime) – andreis11 Apr 07 '20 at 01:30

1 Answers1

0

When you use import datetime, you're importing the datetime module, which contains the datetime class. When you use from datetime import datetime, you're importing just the datetime class from the datetime module. So, if you're only using the datetime class from the datetime module, then from datetime import datetime is fine. Otherwise, if you use import datetime, you'll have to use datetime.datetime to access the datetime class.

ByteDuck
  • 1,821
  • 3
  • 15
  • 27