Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Dates and Times in Python (2014) Dates and Times Manipulating Time Already

gaetano amoroso
gaetano amoroso
2,993 Points

date time in pyttho firsth lesson

what is the differ between datetime.datetime and datetime only? When I call dir on both, I get different results why this? I thought that when I wrote datetime.datetime I'm working on instance directly in implicit way avoiding of writing instance = datetime() and hence datetime.datetime is same instance.datetime. I'm more confuse I have a big headache , can anyone help me?

1 Answer

Chris Freeman
Chris Freeman
Treehouse Moderator 68,423 Points

Hi Gaetano! I remember this confusing me when I first met datetime. The difference is just a single datetime refers to the library module datetime and the other reference datetime.datetime refers to the datetime object within the module datetime. In practice, this becomes:

import datetime  # <-- import the module datetime

# to see the object datetime, you need reference the module namespace
datetime.datetime.now()  # <-- run the 'now' method on the 'datetime' object from the 'datetime' namespace. Yikes!

Alternatively, you can import the datetime object and refer to it directly:

from datetime import datetime  # <-- import the datetime object from the module datetime

# Now you can refer to the object datetime directly because it is now in the local namespace
datetime.now()  # <-- run the 'now' method on the 'datetime' object directly

If using the latter example and then try to refer to datetime.datetime you will get the error similar to:

AttributeError: type object 'datetime.datetime' has no attribute 'datetime'

gaetano amoroso
gaetano amoroso
2,993 Points

Simply wonderful response I now I understand how datetime works.

You helped me a lot thank you very much.