Shirish Herwade
Shirish Herwade

Reputation: 11701

'module' object has no attribute 'OrderedDict' error in pymongo

I'm very new to pymongo. In the following code,

db = MySQLdb.connect(DB_HOST,DB_USR,DB_PWD,DB_NAME)
cursor = db.cursor()
query = "SELECT * FROM %s WHERE userid = \"%s\"" % (table, userID) 
cursor.execute(query)
colNames = [i[0] for i in cursor.description]
rows = cursor.fetchall()
objects_list = []
# The below logic makes JSON objet based on fetch MySQL rows.
for row in rows:
    d = collections.OrderedDict()
    index = 0
    for col in colNames:
        d[col] = row[index]
        index = index + 1
    objects_list.append(d)
return objects_list

I'm getting the error,

trngl_advertise_perfm
trngl_advertise_activity
trngl_user_fblike
Traceback (most recent call last):
File "IngestDataToMongo.py", line 83, in <module>
userData = getData(user[0], TABLES[i]) # Get data of each user.
File "IngestDataToMongo.py", line 51, in getData
d = collections.OrderedDict()
AttributeError: 'module' object has no attribute 'OrderedDict'

Please tell me, how to remove the error.

Upvotes: 2

Views: 9418

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1122132

You are using Python 2.6 or earlier. OrderedDict was not added to Python until version 2.7.

From the documentation:

New in version 2.7.

You could use this backport instead (also available from PyPI), it'll work on python versions 2.4 and up, or install python 2.7 and run your script with that version instead.

Upvotes: 11

Related Questions