Cannot use module aioflask(Python). ImportError: cannot import name '_app_ctx_stack' from 'flask.ctx'
Asked Answered
V

2

8

I need to use aioflask for seting webhooks for my telegram-bot. Here my code, where I set webhook:

from aioflask import Flask, request
...

app = Flask(__name__)
...

@app.route('/')
async def webhook():
    await bot.delete_webhook()
    await bot.set_webhook(url=APP_URL)

    return '!', 200
...

But, when I run app, it give me this error:

Traceback (most recent call last):
  File "D:/Python_Projects/FilmMarketBot/check.py", line 1, in <module>
    from aioflask import Flask, request
  File "D:\Python_Projects\FilmMarketBot\venv\lib\site-packages\aioflask\__init__.py", line 2, in <module>
    from .app import Flask
  File "D:\Python_Projects\FilmMarketBot\venv\lib\site-packages\aioflask\app.py", line 14, in <module>
    from .ctx import AppContext, RequestContext
  File "D:\Python_Projects\FilmMarketBot\venv\lib\site-packages\aioflask\ctx.py", line 4, in <module>
    from flask.ctx import AppContext as OriginalAppContext, \
ImportError: cannot import name '_app_ctx_stack' from 'flask.ctx' (D:\Python_Projects\FilmMarketBot\venv\lib\site-packages\flask\ctx.py)

Please, tell how can I fix it. ...Why always me?

Voigt answered 12/8, 2022 at 22:37 Comment(4)
Is Flask installed in your virtual environment?Thrive
Sure. I am not so stupid :)Voigt
I had resolved this problem by installation flask[async] (pip install flask[async]). I recommend don't use aioflask. This package is not working!Voigt
If you want async Flask, you’re better off using Quart anyway since that’s the official async version of Flask.Thrive
S
6

There seems to be a breaking change in Flask 2.2.0, which causes this incompatibility. As a workaround you can downgrade your Flask package to 2.1.3. This change solved the issue for me.

Reported the issue on GitHub: https://github.com/miguelgrinberg/aioflask/issues/10

Sundberg answered 20/8, 2022 at 8:32 Comment(2)
I just want to add for anyone that might be confused why it doesn't work for them. check this question out. #53271146 I had a global flask installation installed, which overrode my flask command. You should be able to see the version with flask --version tooBraid
pip uninstall flask pip install flask==2.1.3Thailand
L
0

I had this same issue for quite some time, and after removing my venv, reinstalling every install required to run my flask app countless of times, I noticed something fishy in the install log.

For context, this was my error:

Error: While importing "Project", an ImportError was raised:
    Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/flask/cli.py", line 240, in locate_app
    __import__(module_name)

  File "/mnt/d/CITS3200_team_14/Project.py", line 4, in <module>
    from app.routes import index, unit

  File "/mnt/d/CITS3200_team_14/app/__init__.py", line 3, in <module>
    from flask_sqlalchemy import SQLAlchemy

  File "/home/hunkhill1/.local/lib/python3.8/site-packages/flask_sqlalchemy/__init__.py", line 5, in <module>

    from .extension import SQLAlchemy
  File "/home/hunkhill1/.local/lib/python3.8/site-packages/flask_sqlalchemy/extension.py", line 22, in <module>

    from .session import _app_ctx_id
  File "/home/hunkhill1/.local/lib/python3.8/site-packages/flask_sqlalchemy/session.py", line 8, in <module>

    from flask.globals import app_ctx
ImportError: cannot import name 'app_ctx' from 'flask.globals' (/usr/lib/python3/dist-packages/flask/globals.py)

And when reinstalling everything from my requirements.txt I got:

ERROR: launchpadlib 1.10.13 requires testresources, which is not installed.

Just above where the sqlalchemy install happens.

Doing some research on it, I found an answer on GitHub:
https://gist.github.com/y56/0540d22a1db40dacc7fbbb93c866821e

And it ran!

I hope this helps, or at least drives you in the right direction to figure out what's wrong :)

Limburger answered 8/9, 2023 at 8:40 Comment(1)
While the link may contain the solution, links may become unavailable. Please edit your answer and add explicitly what you did to make it work.Ides

© 2022 - 2025 — McMap. All rights reserved.