ImportError: cannot import name 'JSONEncoder' from 'flask.json'
Asked Answered
M

3

7

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!

Mallard answered 28/6, 2023 at 7:23 Comment(2)
Does this answer your question ? #76107950Ruzich
thanks for the info, not quite. I saw the post but it didn't make clear to me that I have to downgrade FlaskFeudalism
T
3

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

Tidal answered 28/6, 2023 at 7:31 Comment(1)
ooh, that's why. I saw a post on the mongodb help page (docs.mongoengine.org/projects/flask-mongoengine/en/latest/…) saying they don't have enough developers. Thanks a lot for the info.Feudalism
A
7

It appears the repo is not maintained recently. Instead of degrading flask, you should install flask-mongoengine from this fork https://github.com/idoshr/flask-mongoengine/tree/1.0.1

uninstall current version by

 pip uninstall flask-mongoengine

And install using

pip install git+https://github.com/idoshr/[email protected]

This worked for me. I got this from here https://github.com/MongoEngine/flask-mongoengine/issues/525#issuecomment-1717066429

Auld answered 9/10, 2023 at 15:50 Comment(0)
T
3

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

Tidal answered 28/6, 2023 at 7:31 Comment(1)
ooh, that's why. I saw a post on the mongodb help page (docs.mongoengine.org/projects/flask-mongoengine/en/latest/…) saying they don't have enough developers. Thanks a lot for the info.Feudalism
S
3

I downgrade my flask version and issue solved.

pip install flask==2.2.5

Slop answered 15/11, 2023 at 17:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.