ImportError: No module named objectid
Asked Answered
M

5

16

I have currently django-mongodb-engine 0.4.0 version installed on my Mac OS X 10.6.8 and weirdly encountered an interesting error while importing the 'compiler' module:

>> from django_mongodb_engine import compiler

and I got the following error:

ImportError Traceback (most recent call last)
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django_extensions/management/commands/shell_plus.pyc in <module>()
----> 1 from django_mongodb_engine import compiler

/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django_mongodb_engine/compiler.pyc in <module>()
      15 from pymongo.errors import PyMongoError
      16 from pymongo import ASCENDING, DESCENDING
 ---> 17 from pymongo.objectid import ObjectId, InvalidId
      18 
      19 from djangotoolbox.db.basecompiler import NonrelQuery, NonrelCompiler, \

ImportError: No module named objectid

I installed the package via pip command and all the dependencies like pymongo, djangotoolbox have been installed along with it. The pip command I'd used is this:

>> sudo env ARCHFLAGS='-arch i386 -arch x86_64' pip install --upgrade  django-mongodb-engine

The current versions of the related packages are:

>> pip freeze | grep mongo
django-mongodb-engine==0.4.0
pymongo==2.2

I suspect the django_mongodb_engine package is broken because ObjectId is not imported inside pymongo anymore. Instead, it belongs to bson module.

What are your suggestions to fix this problem? Should I clone the django-mongodb-engine project and fix on github?

Mlawsky answered 14/5, 2012 at 8:19 Comment(0)
S
56

I've ran into something similar, for me the problem was this line:

from pymongo.objectid import ObjectId

The call seems to work in versions of PyMongo < 2.2

In pymongo 2.2 the call to import objectid is:

from bson.objectid import ObjectId
Sacco answered 6/6, 2012 at 18:4 Comment(1)
To clarify, ImportError: No module named bson.objectid will result if you try from bson.objectid import ObjectId without pymongo installed in your environment. If you run pip/conda install pymongo or get it some other way, from bson.objectid import ObjectId should then work.Chivy
P
3

Looks like they're already aware (see this pull request), but no one's created a pull request against the develop branch yet. You could try fixing that and submit a new pull request.

Predisposition answered 14/5, 2012 at 8:35 Comment(1)
I got the patch for this commit. Looks like, it was patched 12 days ago, not in 0.4.0 version. github.com/mgmtech/mongodb-engine/commit/…Mlawsky
C
2

I tried to install bson with pip directly. Uninstall, and install pymongo instead and the error didnt reappear.

Crossland answered 20/11, 2016 at 20:25 Comment(0)
G
2

I'm on Python3 (in 2017) and found that the following works:

from pymongo import MongoClient
from bson import ObjectId
Gallium answered 12/7, 2017 at 13:58 Comment(0)
I
0

for me installing pymongo solved my issue:

pip3 install pymongo

source: https://kb.objectrocket.com/mongo-db/how-to-install-pymongo-and-connect-to-mongodb-in-python-363

Ivett answered 10/11, 2023 at 9:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.