简体   繁体   中英

Why am I getting TypeError: 'module' object is not callable in python?

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM