Reputation: 273
I can import datetime and access datetime.date but when I try to import datetime.date directly I get an import error. Why is this?
>>> import datetime
>>> print datetime.date
<type 'datetime.date'>
>>> import datetime.date
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named date
>>>
https://svn.python.org/projects/sandbox/trunk/datetime/datetime.py
There seems to be a date class in datetime.py and today under that
To make matters more confusing there is datetime.date.today and datetime.datetime.now https://www.codecademy.com/en/forum_questions/523fb72b80ff3325c6000732
Upvotes: 0
Views: 10515
Reputation: 86
>>> from datetime import date
>>> now = date.today
>>> now().month
8
You can use a variable to use the function directly
Upvotes: 3
Reputation: 310
datetime is module Import imports only the package.
To import the class, you can import it from datetime import date
>>> import datetime
>>> import datetime.date
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named 'datetime.date'; 'datetime' is not a package
>>> from datetime import date
>>> print (date)
<class 'datetime.date'>
Upvotes: 2
Reputation: 13642
Use the below syntax : from
>>> from datetime import date
>>> print date
<type 'datetime.date'>
Upvotes: 1
Reputation: 637
try to use
Upvotes: 0