pallets / flask

The Python micro framework for building web applications.

Home Page:https://flask.palletsprojects.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

cannot import name '_request_ctx_stack' from 'flask'

BrianMwas opened this issue · comments

I am trying to run my webserver with Flask and Flask-Socket.io I have set up socket.io and flask right but for some reason when running it with Docker. I get the error: cannot import name '_request_ctx_stack' from 'flask'. Am using Flask version 2.3.3 and the latest Flask-Socket.io

Install both Flask and Flask-Socket.io create a Dockerfile to build the app and add docker-compose to handle the build using docker-compose up.

Here's the code you can use:

from flask import Flask

from flask_socketio import SocketIO
from src.db.init import DbInit
from src.manager.sockets import SocketManager
from src.routes import heath_route as hr
from src.routes import docs_routes as dr
from src.routes import query_route as qr
from decouple import config

app = Flask(__name__)

# Register the blueprints
app.register_blueprint(hr.health_bp, url_prefix='/api')
app.register_blueprint(dr.files_bp, url_prefix='/api')
app.register_blueprint(qr.query_bp, url_prefix='/api')

# SocketIO.app(socketio, app=app, path="/io", cors_allowed_origins="*")
socketio = SocketIO(app, cors_allowed_origins="*")
db = DbInit.get_instance()
manager = SocketManager(socketio, db)


def start_app():
    """Start the application"""
    port = config("PORT", 5050)
    host = config("HOST", "0.0.0.0")
    env = config("ENVIRONMENT", "development")
    if env == "development":
        socketio.run(host=host, app=app, port=port, debug=True, use_reloader=True)
    else:
        socketio.run(host=host, app=app, port=port, debug=False, use_reloader=False)

Here is the traceback

backend  | Traceback (most recent call last):
backend  |   File "/app/main.py", line 1, in <module>
backend  |     from app import start_app
backend  |   File "/app/app.py", line 6, in <module>
backend  |     from flask_socketio import SocketIO
backend  |   File "/usr/local/lib/python3.10/site-packages/flask_socketio/__init__.py", line 19, in <module>
backend  |     from flask import _request_ctx_stack, has_request_context, json as flask_json
backend  | ImportError: cannot import name '_request_ctx_stack' from 'flask' (/usr/local/lib/python3.10/site-packages/flask/__init__.py)

I would like the app to run on the port 5050 and be able to allow POST, handle file uploads.

Environment:

  • Python version: 3.10.13
  • Flask version: 2.3.3

You are not using the latest Flask-SocketIO. The version that you are using is only compatible with older versions of Flask. At some point around Flask 2.2 they have changed some implementation details regarding user sessions in a backwards incompatible way.

Flask-SocketIO 5.3.0 and up have adapted to their new way, so switch to that and hopefully you should be fine.

Thanks @miguelgrinberg let me try that out

Will give feedback here instead of opening a new issue incase something occurs, I had tried adding the allow_wezbierg flag to true and it worked fine but I could not upload files for some reason

Am getting the following error
RuntimeError('The Werkzeug web server is not ' backend | RuntimeError: The Werkzeug web server is not designed to run in production. Pass allow_unsafe_werkzeug=True to the run() method to disable this error.
If I add this flag to the socketio.run function uploading files fails,

I've been in this loop for almost three days now, not sure whether its a combination of the package versions that's wrong or what it is really.

I suggest that you start by closing this issue, since you do not seem to experience the original issue anymore.

The whole point of having the allow_unsafe_werkzeug flag is to prevent you from doing something that is almost always a bad idea. I would consider switching to a production server instead of continuing using Werkzeug. Try Gunicorn, for example.

Finally you seem to have a problem uploading files, which is completely unrelated to the allow_unsafe_werkzeug flag. You suggest that uploads work without this flag set, but that cannot be, since without the flag the web server wouldn't start at all. Once you collect enough information about your file upload issue, you may consider writing a discussions post on the Flask repo or Flask-SocketIO repo, whichever is the project you need help with.