pythonmongodbflaskpython-venv

ImportError: cannot import name 'JSONEncoder' from 'flask.json'


I'm following a course on full-stack with Flask. My init.py looks like:

from flask import Flask
from config import Config
from flask_mongoengine import MongoEngine

app = Flask(__name__)
app.config.from_object(Config)

db = MongoEngine()
db.init_app(app)

from application import routes

However, when importing from flask_mongoengine import MongoEngine, I'm getting an ImportError:

ImportError: cannot import name 'JSONEncoder' from 'flask.json' 

My venv looks like:

blinker==1.6.2
click==8.1.3
colorama==0.4.6
dnspython==2.3.0
email-validator==2.0.0.post2
Flask==2.3.2
flask-mongoengine==1.0.0
Flask-WTF==1.1.1
idna==3.4
itsdangerous==2.1.2
Jinja2==3.1.2
MarkupSafe==2.1.3
mongoengine==0.27.0
pymongo==4.4.0
python-dotenv==1.0.0
Werkzeug==2.3.6
WTForms==3.0.1

Is there anything I can do here to avoid this conflict? Thanks!


Solution

  • flask_mongoengine seems to be not currently maintained and does not work with current Flask versions. If you absolutely must use it, you need to downgrade your Flask version, which may (and likely will) get you into other trouble.

    There is an issue on github regarding your error message: https://github.com/MongoEngine/flask-mongoengine/issues/522

    The deprecation warning came with Flask 2.2.0 in 08/2022: Flask Changes

    After a brief look at the repo, it seems the maintainer was already on it: https://github.com/MongoEngine/flask-mongoengine/blob/master/flask_mongoengine/json.py