Registry / web-framework / flask-injector

flask-injector

JSON →
library0.15.0pypypi✓ verified 86d ago

Adds Injector, a Dependency Injection framework, support to Flask. Current version 0.15.0, maintained irregularly.

pip install flask-injector
INSTALL
IMPORT
SIG · FLASK-INJECTOR
F
flask-injector
web-frameworkpythonv0.15.0
Install
2.2s avg
Import
466ms
Disk
21MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.15.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.488s · 22.6MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 2.2s · import 0.444s · 23MB
21MB installed
● package 21MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

FlaskInjector
from flask_injector import FlaskInjector
from flask.ext.injector import FlaskInjector
Flask extensions deprecated the flask.ext namespace in Flask 2.3.
request
from injector import inject
from flask_injector import inject
The @inject decorator is from the injector library, not flask-injector.

Basic integration with Flask and Injector.

from flask import Flask, jsonify from flask_injector import FlaskInjector from injector import inject, singleton class Greeter: def greet(self, name: str) -> str: return f'Hello, {name}!' app = Flask(__name__) @app.route('/') @inject def home(greeter: Greeter): return jsonify({'greeting': greeter.greet('World')}) def configure(binder): binder.bind(Greeter, to=Greeter, scope=singleton) FlaskInjector(app=app, modules=[configure]) if __name__ == '__main__': app.run()
Debug
Known issues
deprecatedThe 'flask.ext.injector' import pattern is deprecated and will break in Flask 2.3+. Use 'flask_injector' directly.
fix
Replace 'from flask.ext.injector import FlaskInjector' with 'from flask_injector import FlaskInjector'.
affects: 0.14.0+
gotchaInjector uses static type hints for injection. If you omit type hints or use incorrect ones, injection may fail silently.
fix
Always annotate parameters with the exact class type expected, not strings or unions.
affects: all
gotchaYou must call FlaskInjector(app=app) before any request is handled. If called after route registration, dependencies may not be resolved inside views.
fix
Initialize FlaskInjector right after creating the Flask app and before returning it from the factory function.
affects: all
breakingInjector 0.18+ changed the way modules are configured. If you use older patterns like 'modules=[lambda binder: binder.bind(...)]', this still works, but future versions may drop support.
fix
Use a function with a binder parameter, or a class implementing configure().
affects: flask-injector 0.15.0 uses injector ~=0.12
Errors
Common errors & fixes
ImportError: cannot import name 'FlaskInjector' from 'flask.ext.injector'
The flask.ext namespace is deprecated and removed in Flask 2.3.
fix
Use 'from flask_injector import FlaskInjector' instead.
TypeError: 'module' object is not callable when using @inject
Importing @inject from flask_injector instead of injector.
fix
Import inject: from injector import inject
AssertionError: No application found
FlaskInjector not initialized or app object not passed correctly.
fix
Ensure FlaskInjector(app=app) is called after app creation and before first request.
Upgrade
Version history
0.15.0latest on PyPI · released Jul 27, 2023
Audit
Dependencies

No dependency data recorded yet.

Agent activity
6 hits · last 30 days
node
6
Resources
flask-injector — pip install flask-injector · libregistry