From: questions a. <que...@gm...> - 2012-04-23 23:14:56
|
thanks for all the responses. still finding it very confusing!! but got it to work (without having to import in the loop). I used: from datetime import datetime as dt but I also had to call: from datetime import timedelta and now it seems to work nicely. thank you On Fri, Apr 20, 2012 at 3:36 AM, Benjamin Root <ben...@ou...> wrote: > > > 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 > > |