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
muslpy 3.10–3.95 runs
installs and imports cleanly · install 0.0s · import 0.488s · 22.6MB
glibcpy 3.10–3.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.fixReplace '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.fixAlways 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.fixInitialize 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.fixUse 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.
fixUse 'from flask_injector import FlaskInjector' instead.
TypeError: 'module' object is not callable when using @inject
Importing @inject from flask_injector instead of injector.
fixImport inject: from injector import inject
AssertionError: No application found
FlaskInjector not initialized or app object not passed correctly.
fixEnsure 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.