Aravindh R
Aravindh R

Reputation: 61

Flask-SQLAlchemy raises ImportError: cannot import name 'app_ctx' from 'flask.globals'

I have a Flask app with the following requirements.txt:

flask==2.1.3
flask-sqlalchemy

I started getting the following error when I run the app:

ImportError: cannot import name 'app_ctx' from 'flask.globals'

Why did this error start happening, and how do I fix it?

Upvotes: 1

Views: 13764

Answers (4)

Raja Chowdhury
Raja Chowdhury

Reputation: 1

if your flask version is 3.0* then you should use flask-sqlalchemy version 2.5.1 this is will definitely solve the issue

Upvotes: -2

Aravindh R
Aravindh R

Reputation: 61

When you declare flask-sqlalchemy in requirements.txt without a version, it will automatically be updated to latest version. flask-sqlalchemy>=3.0 does not support flask<2.2.

To fix the problem you should upgrade to Flask >= 2.2 as well.

flask==2.2.2
flask-sqlalchemy==3.0.2

Applications should always include ==version for each dependency in their requirements.txt file, and pin all libraries.

Upvotes: 4

davidism
davidism

Reputation: 127340

Flask-SQLAlchemy 3 requires Flask >= 2.2. Update Flask.


Flask < 2.2 and Flask-SQLAlchemy < 3 no longer receive updates, so while you can pin to previous versions instead of upgrading, you should prefer upgrading to continue receiving support.

Upvotes: 2

Zenyate
Zenyate

Reputation: 19

or you can use flask-sqlalchemy==2.5.1 flask-sqlalchemy newest version is 3.0.0, which is pulished on 10/4/2022,if you have to use this version, code change is needed

Upvotes: 0

Related Questions