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

Joshua Kordani jkordani at intlogsys.com
Thu Jan 7 02:51:07 EST 2010


Gary Herron wrote:
> 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?
> 
> Just a little confusion on your part.
> 
> datetime is a module, so you can import it (as you did) and objects from it
> 
> datetime.datetime is a type (like a class is a type) not a module so you 
> cannot import from it.
> 
> 
> So you can
>  import datetime
> and you can
>  from datetime import <whatever>
> but that's all the further you can go with imports.
> 
> Some of the confusion may be from having a class (well actually it's a 
> type) with the same name as its module.  This is now considered bad form.
> 
> You might achieve the effect you were you were trying for like this:
>  import datetime
>  today = datetime.datetime.today
> or
>  from datetime import datetime
>  today = datetime.today
> Then you'll be able to call
>  today()
> at any time.
> 
> Gary Herron
> 
> 
>>
>> Josh
> 

@ All thanks for the responses!  That was the key bit I was missing.

Josh



More information about the Python-list mailing list