问题
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?
回答1:
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
回答2:
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.
回答3:
I tried to install bson with pip directly. Uninstall, and install pymongo instead and the error didnt reappear.
回答4:
I'm on Python3 (in 2017) and found that the following works:
from pymongo import MongoClient
from bson import ObjectId
来源:https://stackoverflow.com/questions/10579704/importerror-no-module-named-objectid