ImportError: cannot import name '_app_ctx_stack' from 'flask' . even though it did not imported it

192 Views Asked by At
from flask import Flask,request , render_template , redirect , url_for
from flask_login import LoginManager , UserMixin , login_user , login_required , logout_user , current_user 
from flask_mysqldb import MySQL
from flask_bcrypt import Bcrypt

i am trying to make a login and signup page using flask. but its giving error that

prakash@prakash-laptop:~/Desktop/flask/auth_$ python3 app.py
Traceback (most recent call last):
  File "/home/prakash/Desktop/flask/auth_/app.py", line 4, in <module>
    from flask_mysqldb import MySQL
  File "/usr/local/lib/python3.10/dist-packages/flask_mysqldb/__init__.py", line 3, in <module>
    from flask import _app_ctx_stack, current_app
ImportError: cannot import name '_app_ctx_stack' from 'flask' (/home/prakash/.local/lib/python3.10/site-packages/flask/__init__.py)

i had not imported " _app_ctx_stack" then also its showing error

i tried to reinstall flask_login and flask

1

There are 1 best solutions below

3
Prateek p On

The error is clearly in flask_mysqldb . Perhaps the module has errors? Its not a very big project, and might have fallen out of support. Flask's most recent version was on feb 4th of 2024. flask_mysqldb was on oct 5th 2023. I highly suggest using mysql-connector-python instead.

Edit: https://github.com/miguelgrinberg/Flask-SocketIO/issues/1856 and https://github.com/fedora-copr/flask-whooshee/issues/71 both tell us that '_app_ctx_stack' has been deprecated. It is still in use in flask_mysqldb though. This seems to further reinforce my point above

From the Flask changelog:

The app and request contexts are managed using Python context vars directly rather than Werkzeug’s LocalStack. This should result in better performance and memory use. #4682

Extension maintainers, be aware that _app_ctx_stack.top and _request_ctx_stack.top
are deprecated. Store data on g instead using a unique prefix, like g._extension_name_attr.