-2

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.

Community
  • 1
  • 1
Calaf
  • 10,113
  • 15
  • 57
  • 120
  • Pymongo does in the required `bson` package and also imported in [`pymongo`](http://api.mongodb.org/python/1.7/api/pymongo/objectid.html) package. Looks like you might be missing some imports. This is [mongoalchemy](http://www.mongoalchemy.org/) anyway. Your install might have some issues. – Neil Lunn Apr 06 '16 at 01:58
  • Would you care to clarify? The website uses "from flask.ext.mongoalchemy import MongoAlchemy". It does not use bson. https://github.com/cobrateam/flask-mongoalchemy/blob/master/examples/library/library.py but – Calaf Apr 06 '16 at 02:38
  • How did you "install" the `MongoAlchemy` module? The error is suggesting the required dependencies are not installed. – Neil Lunn Apr 06 '16 at 02:50
  • `mongoalchemy` is installed fine. `port installed | grep mongoalchemy` reports `py27-flask-mongoalchemy @0.5.3_0 (active)` and `py27-mongoalchemy @0.11_0 (active)`. – Calaf Apr 06 '16 at 02:56
  • I said "how" **not** "if". Again, you appear to be missing required dependencies. You really should not be installing via `ports` anyway, and should use a "local" installed and package managed python for your projects rather than the "system" distribution. – Neil Lunn Apr 06 '16 at 03:07
  • I wasn't clear. I never installed mongoalchemy myself. MacPorts has a dependency system that picked it up along the way. By "local" do you mean `/usr/bin/python`? That one is 2.7.10 so it's not too bad. – Calaf Apr 06 '16 at 03:18
  • Read up on [pyenv](https://github.com/yyuu/pyenv) and other similar environment management tools that allow you to use a specific build and packages for your own account or better yet "project". You should never mess with the "system" installed python or other installed language interpreter. – Neil Lunn Apr 06 '16 at 03:28

1 Answers1

1

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

basaundi
  • 1,725
  • 1
  • 13
  • 20
  • Nice catch. And it's from 2012 even. If you're on OS X, please recommend a way to install the whole set. I've just installed homebrew. I was able to install mongodb using brew, but brew knows neither flask nor mongoalchemy. – Calaf Apr 06 '16 at 03:25
  • Thanks for your help. virtualenv solved the problem. Weirdly, virtualenv itself, along with (AFAICT) all the modules, came from MacPorts, but as long as it works,... – Calaf Apr 06 '16 at 03:41