pymongo 应该有一个模块 objectid 吗?

Should pymongo have a module objectid?

精简版

全新安装 flask 和 mongoalchemy 后两行:

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

失败:

ImportError: No module named objectid

我应该看哪里?

详情

在 El Capitan 之后:

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

我确认 MacPorts 安装的版本:

> 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)

我是 运行 MacPorts Python:

> which python
/opt/local/bin/python

并且它是 2.7 分支上的最新版本:

> python --version
Python 2.7.11

但是 this 页的第二行:

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

失败:

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

pymongo 应该有一个模块 objectid 吗?

之前对类似问题的讨论 (, 2, 3, 4) 无关紧要。

编辑

嗯... mongodb 本身并未安装为 flask-mongoalchemy 的依赖项。好的,让我们手动完成:

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

这没用。弹出同样的错误。

你有一个古老的 mongoalchemy。

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

也许你不应该相信 python 模块上的 macports。

编辑:

我看到 macports 不再维护该端口: https://www.macports.org/ports.php?by=name&substr=mongoalchemy