2

I have noticed while reading up on python datetime module there are times when responses will show

from datetime import datetime (or date or time)

and I'm not sure why I should or would do this. What is the rational behind this or could someone point me to some resources that might explain this?

2
  • 1
    As opposed to what? I usually do import datetime, but if you're only going to use one class from there, it saves typing. Commented Jul 20, 2022 at 18:59
  • 1
    the datetime module has 3 inner modules that are typically imported. you can import them by saying from datetime import date or from datetime import datetime. this is the same as import datetime.datetime or import datetime.date or import datetime.time. the three inner modules of datetime are what are being shown as 'optional' imports. there isn't an actual syntax for importing where you can say import this OR import that Commented Jul 20, 2022 at 19:02

4 Answers 4

3

The datetime module defines a bunch of different classes. If you just use

import datetime

Then you have to write things like datetime.datetime.strptime() -- the first datetime is the module name, the second is the class name. By using the from syntax you can shorten this to just datetime.strptime().

Sign up to request clarification or add additional context in comments.

1 Comment

that makes sense, thanks for the clarity on the subject
1

The datetime module has multiple classes inside of it. Though at first the name may seem confusing, when you compare it to another module, like random, it starts to make more sense.

If we are only going to use randint from the random module, we don't want to import the whole random module so we will do something like this: from random import randint

It's the same concept with datetime except the only part of datetime that we want is the datetime class. Inside of the module datetime there is a datetime class, just how in the module random there is a randint function.

If you wanted to import the whole random module then use randint you would do:

import random
random_number = random.randint(0, 5)

Just as if you wanted to import the whole datetime module, then use the datetime class you'd do:

import datetime
now = datetime.datetime.now()

1 Comment

Great explanation. After looking at the module documentation some more and doing some testing, I see now. Yes the datetime module and datetime type is confusing.
0

The first datetime refers to the module, whereas the second is a type defined therein. Depending on the application you can choose to import datetime.datetime, datetime.date or datetime.time.

More information can be found at the link

1 Comment

that makes sense, thanks for the clarity on the subject. Yes I have been reading the python documentation on the datetime module, but you and Barmar have brought clarity on what the documentation is saying
0

Datetime


Module Reference

datetime


Disambiguation: datetime, is a builtin Python library (package, module), of the division of Data Types. It is basically a file that is named that way:

datetime.py

There are classes in that file, usually called module/class attributes.


File structure

├── datetime.py
        ├── class date(builtins.object)
        ├── class time(builtins.object)
        └── class datetime(date)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.