13

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?

Ozgur Vatansever
  • 49,246
  • 17
  • 84
  • 119

4 Answers4

53

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
Vlad
  • 691
  • 8
  • 13
  • 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. – ximiki Oct 11 '18 at 16:21
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.

Dominic Rodger
  • 97,747
  • 36
  • 197
  • 212
  • I got the patch for this commit. Looks like, it was patched 12 days ago, not in 0.4.0 version. https://github.com/mgmtech/mongodb-engine/commit/d3cba0d013350d5b085bed9ea440a04b232f8575 – Ozgur Vatansever May 14 '12 at 08:42
2

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

David
  • 3,166
  • 2
  • 30
  • 51
1

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

from pymongo import MongoClient
from bson import ObjectId
Shane Reustle
  • 8,633
  • 8
  • 40
  • 51