Search code examples
pythonmongodbflaskosx-yosemitemacports

Should pymongo have a module objectid?


Short Version

After a fresh install of flask and mongoalchemy the two lines:

from flask import Flask
from flask.ext.mongoalchemy import MongoAlchemy

fail with:

ImportError: No module named objectid

Where should I look?

Details

On El Capitan after:

> sudo port install py27-flask py27-flask-mongoalchemy

I confirm the versions that MacPorts has installed:

> port installed | grep 'flask\|mongo'
  py27-flask @0.10.1_1 (active)
  py27-flask-mongoalchemy @0.5.3_0 (active)
  py27-flask-script @2.0.5_0 (active)
  py27-mongoalchemy @0.11_0 (active)
  py27-pymongo @3.2.2_0 (active)

that I'm running the MacPorts Python:

> which python
/opt/local/bin/python

and that it's the most recent on the 2.7 branch:

> python --version
Python 2.7.11

But then the very second line at this page:

from flask import Flask
from flask.ext.mongoalchemy import MongoAlchemy

fails with:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/flask/exthook.py", line 81, in load_module
    reraise(exc_type, exc_value, tb.tb_next)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/flaskext/mongoalchemy/__init__.py", line 14, in <module>
    from mongoalchemy import document, exceptions, fields, session, query
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/mongoalchemy/document.py", line 45, in <module>
    from mongoalchemy.fields import ObjectIdField, Field, BadValueException, SCALAR_MODIFIERS
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/mongoalchemy/fields.py", line 60, in <module>
    from pymongo.objectid import ObjectId
ImportError: No module named objectid

Should pymongo have a module objectid?

The previous discussions of a similar issue (1, 2, 3, 4) are irrelevant.

Edit

Hmm... mongodb itself was not installed as a dependency of flask-mongoalchemy. OK, let's do it manually:

> port list | grep mongodb
mongodb                        @3.2.4          databases/mongodb
> sudo port install mongodb

That doesn't help. The same error pops up.


Solution

  • You have an old mongoalchemy.

    See commit "Use bson package instead of deprecated (then removed) pymongo ones": https://github.com/jeffjenkins/MongoAlchemy/commit/9152d83a8515fd557b2d90fb0059fa42a24730fe

    Maybe you shouldn't trust macports on python modules.

    Edit:

    I see the port is no longer maintained at macports: https://www.macports.org/ports.php?by=name&substr=mongoalchemy