Flask
Flask is a lightweight WSGI web application framework in Python, designed with simplicity and flexibility in mind. The current version is 3.1.3, released on March 28, 2026, following a regular release cadence with feature and fix updates as needed.
Common errors
-
ModuleNotFoundError: No module named 'flask'
cause The Flask library is not installed in the Python environment where the code is being executed, or the wrong Python interpreter/virtual environment is active.fixInstall Flask using pip: `pip install Flask`. If using a virtual environment, ensure it is activated before installation. -
RuntimeError: Working outside of application context
cause This error occurs when Flask functionality that relies on an active application context (like `current_app`, `g`, or database operations with extensions like Flask-SQLAlchemy) is accessed outside of a request or CLI command.fixManually push an application context using `with app.app_context():` around the code block that requires it. -
TypeError: The view function did not return a valid response
cause A Flask view function must return a valid response object, string, tuple, or an instance of `werkzeug.exceptions.HTTPException`. This error occurs when the function implicitly returns `None` or ends without an explicit return statement.fixEnsure that every execution path within a view function explicitly returns a valid Flask response, such as a string, a `render_template()` call, a `jsonify()` response, or a tuple like `('Error message', 400)`. -
Method Not Allowed (405 Error)
cause The HTTP method used in the client's request (e.g., POST, PUT, DELETE) is not permitted for the requested URL by the Flask route definition. By default, Flask routes only accept GET requests.fixExplicitly specify the allowed HTTP methods in the route decorator using the `methods` argument: `@app.route('/my_route', methods=['GET', 'POST'])`.
Warnings
- breaking Flask 3.1.3 includes a security fix that may affect session behavior. Review the release notes for details.
- gotcha Relative imports can cause issues if not used correctly. Ensure your project structure supports them to avoid ImportError.
- breaking The application experienced a timeout during testing. This can be caused by infinite loops, deadlocks, resource exhaustion, or long-running operations that exceed the test runner's timeout threshold.
Install
-
pip install flask
Imports
- Flask
from flask import Flask
- render_template
from flask import render_template
- request
from flask import request
Quickstart
from flask import Flask, render_template, request
app = Flask(__name__)
@app.route('/')
def index():
return render_template('index.html')
if __name__ == '__main__':
app.run(debug=True)