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
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
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.