Answer a question

I have a python flask based app, which writes to mongo database.

I am using python 3 with the following requirements.txt

Flask
Jinja2
Werkzeug
certifi
chardet
gunicorn
requests
flask_restful
flask
pymongo

I run into this issue :

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'

I am trying to understand why does this happen?

This section is what I am concerned with

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'

Most of the internet blogs/links tell me bson and pymongo dont play well with eachother. pymongo has its own version of bson and i dont need it in my requirements.txt.

I am looking for help to see why this happens, and how to remidiate this.


Update #1

I was using pymongo-3.5.1, and thats where the problem persists. I just downgraded to pymongo-3.5.0 and the problem vanished.

This means I know the workaround to fix this, but the upstream code/package has some issues which needs fixing.

Answers

It might be some package misconfiguration going on. If the bson package is from the mongo-python-driver, which is likely, since in line 19, the import statement from bson.py3compat import string_type, PY3, text_type is present.

However the file py3compat.py seems to be missing, which should be right next to code.py in the bson path.

Update

After the question's update letting us know a downgrade fixed the issue and inspecting the code changes between both versions, no relevant changes to the issue are identifiable. Therefore it even more so suggests there was something wrong with the modules, what exactly is hard to tell.

Logo

MongoDB社区为您提供最前沿的新闻资讯和知识内容

更多推荐