Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pymongo import errors in python3 flask application

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.

like image 817
sudhishkr Avatar asked Jul 22 '26 03:07

sudhishkr


1 Answers

Try to uninstall both bson and pymongo:

sudo pip uninstall bson
sudo pip uninstall pymongo

and then reinstall pymongo using pip:

sudo pip install pymongo
like image 148
jdoe Avatar answered Jul 23 '26 16:07

jdoe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!