1

I'm creating a string that I an then use in a method that queries a mongodb collection. Eventually the dates will be from user input. Here's the relevant code and string:

import pymongo
from pymongo import MongoClient
from datetime import datetime
import time
import datetime
start_yr    = 2015
start_mnth  = 2
start_day   = 1
end_yr      = 2015
end_mnth    = 2
end_day     = 28

# this is the line called in the error
created_at_string = { "created_at": {"$gte" : datetime(start_yr, start_mnth, start_day),"$lt" : datetime(end_yr, end_mnth, end_day)}}

The idea will be to use created_at_string as an argument in more complex query methods.

I'm getting:

Traceback (most recent call last):
  File "main.py", line 218, in <module>
    program.runProgram()
  File "main.py", line 61, in runProgram
    report.RcreateReport()
  File "/filepath/report.py", line 95, in RcreateReport
 created_at_string = { "created_at": {"$gte" : datetime(start_yr, start_mnth, start_day),"$lt" : datetime(end_yr, end_mnth, end_day)}}
TypeError: 'module' object is not callable

Why?

4
  • 3
    Are you sure you did from datetime import datetime and not just import datetime? Commented May 26, 2015 at 21:44
  • 1
    Is that your whole code and is there a typo? I bet you did import datetime and tried to call the module... Commented May 26, 2015 at 21:44
  • 3
    Also, the full traceback could be nice. :) Commented May 26, 2015 at 21:44
  • Thanks everyone. I added the full traceback and double checked my imports. I have from datetime import datetime and then import time and then import datetime Commented May 26, 2015 at 21:53

1 Answer 1

6

I've found your issue:

from datetime import datetime
import time
import datetime 

Let's look at this in order:

In your globals, you have something called datetime, a function. Then, you import time, a module object. Then, you import datetime, hence overwriting your datetime function. Here's an example:

>>> from datetime import datetime
>>> datetime(2015, 05, 26)
datetime.datetime(2015, 5, 26, 0, 0)
>>> import datetime
>>> datetime(2015, 05, 26)

Traceback (most recent call last):
  File "<pyshell#24>", line 1, in <module>
    datetime(2015, 05, 26)
TypeError: 'module' object is not callable
>>> 

No matter what, even if you change the order, you will overwrite something, be it the function or module objects. So, just rename something:

import datetime
import time
from datetime import datetime as dt
Sign up to request clarification or add additional context in comments.

2 Comments

I think this is it. To complete the thought, in the query string, I would have to replace datetime with dt, correct?
That's correct. Otherwise, you'd be calling a module like before! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.