from datetime.datetime import today not working. python2.6.4 on windows

Steve Holden steve at holdenweb.com
Thu Jan 7 01:12:21 EST 2010


Joshua Kordani wrote:
> Greetings all!
> 
> So I'm reading through the manual and I get to the point where it talks
> about packages and how to import them.  namely section 6.4 in the
> tutorial.  I wont repeat the section here, but I want to understand
> whats going on in the following (as typed on my computer).
> 
> Python 2.6.4 (r264:75708, Oct 26 2009, 08:23:19) [MSC v.1500 32 bit
> (Intel)] on
> win32
> Type "help", "copyright", "credits" or "license" for more information.
>>>> import datetime
>>>> dir(datetime)
> ['MAXYEAR', 'MINYEAR', '__doc__', '__name__', '__package__', 'date',
> 'datetime',
>  'datetime_CAPI', 'time', 'timedelta', 'tzinfo']
>>>> dir(datetime.datetime)
> ['__add__', '__class__', '__delattr__', '__doc__', '__eq__',
> '__format__', '__ge
> __', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__',
> '__lt__', '
> __ne__', '__new__', '__radd__', '__reduce__', '__reduce_ex__',
> '__repr__', '__rs
> ub__', '__setattr__', '__sizeof__', '__str__', '__sub__',
> '__subclasshook__', 'a
> stimezone', 'combine', 'ctime', 'date', 'day', 'dst', 'fromordinal',
> 'fromtimest
> amp', 'hour', 'isocalendar', 'isoformat', 'isoweekday', 'max',
> 'microsecond', 'm
> in', 'minute', 'month', 'now', 'replace', 'resolution', 'second',
> 'strftime', 's
> trptime', 'time', 'timetuple', 'timetz', 'today', 'toordinal', 'tzinfo',
> 'tzname
> ', 'utcfromtimestamp', 'utcnow', 'utcoffset', 'utctimetuple', 'weekday',
> 'year']
> 
>>>> from datetime.datetime import today
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> ImportError: No module named datetime
>>>>
> 
> so dir on datetime shows symbols date, time, datetime,etc
> dir on datetime shows today, now, etc
> 
> lets say for arguments sake that I want to just import the today
> function, according to the documentation, the line should be:
> from datetime.datetime import today.
> 
> as you can see, that didn't work.  why not?
> 

Because datetime is a module, but datetime.datetime is a class. You can
import individual names from a module, but a class is a monolithic
all-or-nothing chunk.

There's the potential for confusion because Python also has "packages",
which are like modules but more structured: you can import a module from
a package:

>>> from xml import etree

or import the package module directly:

>>> import xml.etree)

You can also import a submodule from the module:

>>> from xml.etree import cElementTree

or import the submodule directly

>>> import xml.etree.cElementTree

You can even import a class from the submodule:

>>> from xml.etree.cElementTree import Element

but what you *can't* do is import the submodule's class directly:

>>> import xml.etree.cElementTree.Element
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named Element

However, what you did was import the datetime module (which is not a
package, and does not therefore contains submodules), and reference the
datetime class within that module (datetime.datetime), which is all OK.
But datetime.datetime is a class, not a module, so you can't import
anything from it. Neither can you import it directly:

>>> import datetime.datetime
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named datetime

(in the last line, the name "datetime" refers to the class, not themodule.

Hope this helps. You may learn a bit more by actually looking at the
source of the module, which probably lives in

C:\Python26\Lib\datetime.py

regards
 Steve
-- 
Steve Holden           +1 571 484 6266   +1 800 494 3119
PyCon is coming! Atlanta, Feb 2010  http://us.pycon.org/
Holden Web LLC                 http://www.holdenweb.com/
UPCOMING EVENTS:        http://holdenweb.eventbrite.com/




More information about the Python-list mailing list