Is there a conflict of libraries here?
Cameron Simpson
cs at cskk.id.au
Fri Nov 6 04:30:47 EST 2020
On 06Nov2020 09:36, Frank Millman <frank at chagford.com> wrote:
>On 2020-11-06 9:25 AM, Steve wrote:
>>In my program, I have the following lines of code:
>> import datetime
>> from datetime import timedelta
>>
>> from time import gmtime, strftime ##define strftime as time/date right
>>now
>>If I add the code:
>>
>> from datetime import datetime
>>
>>these new lines work:
>>
>> dt = datetime.fromisoformat(ItemDateTime)
>>
>> dt_string = dt.strftime(' at %H:%M on %A %d %B %Y')
>>
>>and will fail without that "datetime import datetime" line
Right, because you're using the datetime _class_ to get the
fromisoformat factory function.
>>however;
>>
>>
>>With that "datetime import datetime" line included,
>>
>>all of the lines of code throughout the program that contain
>>"datetime.datetime" fail.
>>These have been in use for over three years and there are at least a dozen
>>of them.
Yeah. because _those_ lines are using the name "datetime" as the module
name.
Just replace "datetime.datetime" throughout with "datetime". Your code
will be more readable anyway.
>>The error produced is:
>>
>> time1 = datetime.datetime.strptime(T1, date_format)
>>
>> AttributeError: type object 'datetime.datetime' has no attribute
>>'datetime'
That is because "datetime" is currently the class, not the module.
>1. Remove the line 'from datetime import datetime'.
>
>2. Change dt = datetime.fromisoformat(ItemDateTime) to
> dt = datetime.datetime.fromisoformat(ItemDateTime)
>
>Unless I have missed something, that should work.
That will work, but produces verbose code. I prefer to import things
_from_ the datetime module, letting me drop the 'datetime." module
prefix across the code.
Cheers,
Cameron Simpson <cs at cskk.id.au>
More information about the Python-list
mailing list