python3 flask 应用程序中的 pymongo 导入错误

pymongo import errors in python3 flask application

我有一个基于 python flask 的应用程序,它写入 mongo 数据库。

我正在使用 python 3 和以下 requirements.txt

Flask
Jinja2
Werkzeug
certifi
chardet
gunicorn
requests
flask_restful
flask
pymongo

我 运行 进入这个问题:

Traceback (most recent call last):
File "bin/app.py", line 4, in <module>
from helper import save_message_2_db
File "/app/bin/helper.py", line 1, in <module>
from database import DataAccess, MongoDB
File "/app/bin/database.py", line 1, in <module>
import pymongo
File "/app/.heroku/python/lib/python3.6/site-packages/pymongo/__init__.py", line 83, in <module>
from pymongo.collection import ReturnDocument
File "/app/.heroku/python/lib/python3.6/site-packages/pymongo/collection.py", line 21, in <module>
from bson.code import Code
File "/app/.heroku/python/lib/python3.6/site-packages/bson/code.py", line 19, in <module>
from bson.py3compat import string_type, PY3, text_type
ModuleNotFoundError: No module named 'bson.py3compat'

我想了解为什么会这样?

这一段是我关心的

File "/app/.heroku/python/lib/python3.6/site-packages/pymongo/__init__.py", line 83, in <module>
from pymongo.collection import ReturnDocument
File "/app/.heroku/python/lib/python3.6/site-packages/pymongo/collection.py", line 21, in <module>
from bson.code import Code
File "/app/.heroku/python/lib/python3.6/site-packages/bson/code.py", line 19, in <module>
from bson.py3compat import string_type, PY3, text_type
ModuleNotFoundError: No module named 'bson.py3compat'

大多数互联网 blogs/links 告诉我 bsonpymongo 彼此相处得不好。 pymongo 有自己的 bson 版本,我的 requirements.txt.

不需要它

我正在寻求帮助以了解为什么会发生这种情况,以及如何解决这个问题。


更新#1

我使用的是 pymongo-3.5.1,问题仍然存在。我刚降级到 pymongo-3.5.0,问题就消失了。

这意味着我知道解决这个问题的方法,但是上游 code/package 有一些问题需要解决。

尝试同时卸载 bsonpymongo:

sudo pip uninstall bson
sudo pip uninstall pymongo

然后重新安装 pymongo 使用 pip:

sudo pip install pymongo

可能是某些软件包配置错误。如果 bson 包来自 mongo-python-driver,这很可能,因为在第 19 行中,存在导入语句 from bson.py3compat import string_type, PY3, text_type

但是文件 py3compat.py 似乎丢失了,它应该就在 bson 路径中的 code.py 旁边。

更新

在问题的更新让我们知道降级解决了问题并检查了两个版本之间的 code changes 之后,没有发现问题的相关更改。因此,它更表明模块有问题,具体是什么很难说。

@bauman.space provided the most informative and useful explanation of this bson/ pymongo import issue in 主题:

pymongo>=2.7.1 directive overwrites your bson installation

from https://github.com/mongodb/mongo-python-driver

Do not install the "bson" package from pypi. PyMongo comes with its own bson package; doing easy_install bson or pip install bson installs a third-party package that is incompatible with PyMongo.

assuming you really want to use the 3rd party bson, you'll need to

pip uninstall bson (removes the overwritten bson that landed via pymongo package) pip install bson (reinstalls the py-bson package) or you could patch the pymongo install to remove the collections.abc line (evidently you don't really need it?), but messing with production packaged code is questionable.

pymongo module https://github.com/mongodb/mongo-python-driver/blob/master/bson/py3compat.py#L22-L25

py-bson module you are trying to use. https://github.com/py-bson/bson/blob/master/bson/py3compat.py#L22-L24

同时卸载 bsonpymongo。然后,先安装 bson,再安装 pymongo