user324747
user324747

Reputation: 273

ImportError: No module named date

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

Answers (4)

Operation420.net
Operation420.net

Reputation: 86

>>> from datetime import date
>>> now = date.today
>>> now().month
8

You can use a variable to use the function directly

Upvotes: 3

character
character

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

nehem
nehem

Reputation: 13642

Use the below syntax : from

>>> from datetime import date
>>> print date
<type 'datetime.date'>

Upvotes: 1

White
White

Reputation: 637

try to use

  • datetime.date.today() with import datetime
  • date.today() with from datetime import date

Upvotes: 0

Related Questions