From: Benjamin R. <ben...@ou...> - 2012-04-19 17:36:40
|
On Thu, Apr 19, 2012 at 1:21 PM, Goyo <goy...@gm...> wrote: > El día 19 de abril de 2012 05:31, questions anon > <que...@gm...> escribió: > > Thank you, I was able to get it to work but only if I imported datetime > > within the loop, otherwise I ended up with the > > AttributeError: type object 'datetime.datetime' has no attribute > 'datetime' > > and if I added 'import datetime' at the top of my script it had an error > > where I loop through combining each month > > " stop_month = datetime(2011, 03, 01) > > TypeError: 'module' object is not callable" > > If you can write a standalone, minimal executable script which > reproduces the problem I'll take a look. Send it as an attachement and > add sample data files if necessary. > > Goyo > > The issue is that there is a slight mixup in namespaces. There is a module called datetime, and that module contains a class object called datetime. So, if your imports at the top are "import datetime", then all your module-related stuff need to be prepended with "datetime.". But, if your imports at the top are "from datetime import datetime", then you can use the object freely, but you can't use anything else from the module unless you also import it. Here is the tricky part. In your code, you did the following: from datetime import datetime If you then did: import datetime depending on the order the two were, one would overwrite the other. You can only have one thing called "datetime". Personally, I would do one of two things: import datetime as dt and use "dt.datetime()" to create datetime objects as well as call functions like "dt.strftime()". Or, do from datetime import datetime, date, timedelta, strftime and get replace calls like "datetime.datetime()" and "datetime.strftime()" with just "datetime()" and "strftime()". I hope that clears things up. Namespaces are a honking good idea, but having objects be the same exact name as a module gets confusing very easily. Cheers! Ben Root |