App Importer
Relevant source files
The following files were used as context for generating this wiki page:
The App Importer is a critical internal component of Uvicorn responsible for dynamically locating and loading ASGI or WSGI applications from string identifiers. It translates user-provided import strings, typically passed via the command line or programmatic configuration, into executable Python objects.
This system ensures that Uvicorn can interface with a wide variety of frameworks by supporting both direct application instances and application factories. It handles path resolution, module importing, and error reporting when applications cannot be located or fail to load.
Sources: uvicorn/config.py:11, uvicorn/main.py:112-113
Architecture and Logic
The importing logic is primarily centered around the import_from_string function, which is utilized by the Config class to resolve the app attribute. This process occurs during the server initialization phase, specifically within the Config.load() and Config.load_app() methods.
Import String Format
Uvicorn expects application identifiers in the format module:attribute. If a user provides an incomplete string (e.g., just the module name without the colon), the importer raises an ImportFromStringError.
Sources: uvicorn/importer.py:1-25, tests/test_cli.py:104-112
Loading Process Flow
The following diagram illustrates the sequence of events when Uvicorn attempts to load an application defined by an import string.
The workflow ensures that errors are caught early, providing specific feedback if the module is missing or the specific attribute is not found within that module.
Sources: uvicorn/importer.py:10-30, uvicorn/config.py:440-445
Application vs. Factory Loading
Uvicorn distinguishes between a static application instance and an application factory (a callable that returns an application).
- Application Instance: A direct reference to an ASGI/WSGI object.
- Application Factory: A callable that Uvicorn must execute to obtain the actual application object. This is explicitly enabled using the
--factoryflag.
Sources: uvicorn/config.py:465-478, uvicorn/main.py:270-276
Component Interaction Table
| Component | Responsibility | Relevant Files |
|---|---|---|
import_from_string | Low-level logic to split strings and import via importlib. | uvicorn/importer.py |
Config.load_app | High-level wrapper that handles ImportFromStringError and exits the process on failure. | uvicorn/config.py |
Config.load | Executes the app (if it's a factory) and wraps it in appropriate middleware (ASGI2, WSGI, etc.). | uvicorn/config.py |
main (CLI) | Parses the app argument and app-dir to modify sys.path before importing. | uvicorn/main.py |
Path Resolution and Environment
The importer's ability to find modules depends on the Python path (sys.path). Uvicorn provides the --app-dir option to allow users to specify where the importer should look for the application module.
Search Path Modification
When app_dir is provided, Uvicorn inserts this directory at the beginning of sys.path before attempting to load the application. This allows importing modules relative to the specified directory.
if app_dir is not None:
sys.path.insert(0, app_dir)
Sources: uvicorn/main.py:456-457, tests/test_cli.py:155-165
Error Handling
The system implements specific exception types to provide clear feedback to the user when imports fail.
| Exception | Cause | Result |
|---|---|---|
ImportFromStringError | Missing colon in string or attribute not found in module. | Logs error and calls sys.exit(1). |
ImportError | The module specified before the colon cannot be found. | Propagates up or caught by Config.load. |
TypeError | Calling an app factory that requires arguments. | Logs factory error and exits. |
Sources: uvicorn/importer.py:3-8, uvicorn/config.py:442-445, tests/test_config.py:133-145
Summary
The App Importer module provides the essential glue between Uvicorn's server core and the various web applications it hosts. By using importlib and structured string parsing, it enables a decoupled architecture where the server does not need prior knowledge of the application's structure. It supports modern development workflows by handling factories, custom search paths, and providing detailed error diagnostics during the critical startup phase.