Registry / web-framework / svcs
library26.2.0pypypi✓ verified 24d ago

svcs is a flexible service locator for Python applications, simplifying dependency management and resource cleanup. It currently stands at version 25.1.0 and typically releases new versions every 1-2 months, incorporating bug fixes, minor features, and framework integrations.

pip install svcs
INSTALL
IMPORT
SIG · SVCS
S
svcs
web-frameworkpythonv26.2.0
Install
1.7s avg
Import
380ms
Disk
17MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v26.2.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.410s · 18.8MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 1.7s · import 0.350s · 19MB
17MB installed
● package 17MB
Code
Verified usage

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

Registry
from svcs import Registry
Container
from svcs import Container
svcs_from_factory
from svcs import svcs_from_factory
Commonly used for framework integrations or when explicit registration of a callable factory is needed.

This quickstart demonstrates how to define a service, register it with `svcs.Registry` using a factory function, and then acquire and use it via `svcs.Container`. It also shows the recommended practice of using a `with` statement for automatic container cleanup.

import svcs class MyService: def __init__(self, name: str): self.name = name def hello(self) -> str: return f"Hello from {self.name}!" def create_my_service() -> MyService: return MyService(name="quickstart_service") # 1. Create a registry and register your service factory reg = svcs.Registry() reg.register_factory(MyService, create_my_service) # 2. Get a container, ensuring it's closed properly with svcs.Container(reg) as container: # 3. Acquire the service service = container.get(MyService) print(service.hello())
Debug
Known issues
breakingThe handling of context managers passed to `register_value()` changed in version 23.21.0. Previously, `svcs` would NOT enter/exit context managers registered this way. Post 23.21.0, `svcs` WILL automatically enter and exit them by default.
fix
If you were passing context managers to `register_value()` and managing their lifecycle manually, explicitly add `enter=False` to `register_value()` calls to maintain the old behavior, or switch to `register_factory()` if you want `svcs` to manage it.
affects: >=23.21.0
gotchaFailing to close `svcs.Container` or `svcs.Registry` instances with pending cleanups will raise a `ResourceWarning`.
fix
Always ensure containers are properly closed. The recommended way is to use `with svcs.Container(...) as container:` for automatic cleanup. For registries, ensure any registered context managers or asynchronous cleanups are explicitly managed.
affects: >=23.15.0
Errors
Common errors & fixes
RuntimeError: No such service: <class 'my_app.Foo'>
You are attempting to retrieve a service type or key that has not been registered with the `svcs` container, or has been unregistered.
fix
Ensure the service is registered using `svcs.Container.register()` or `svcs.Container.register_factory()` before attempting to retrieve it with `svcs.Container.get()`.
ModuleNotFoundError: No module named 'svcs.fastapi'
The optional `fastapi` extra for `svcs` was not installed, or the submodule is being accessed without being properly imported.
fix
Install the required extra using `pip install "svcs[fastapi]"`, and ensure you `from svcs.fastapi import svcs_from_request` when using it.
svcs.exceptions.ServiceError: Factory returned an object that is not an async context manager.
An asynchronous service factory was registered, but the object it returned does not implement the async context manager protocol (`__aenter__` and `__aexit__`).
fix
Ensure the class or object returned by the async factory implements `async def __aenter__(self):` and `async def __aexit__(self, exc_type, exc_val, exc_tb):` methods.
RuntimeError: There are no services active outside of a Context.
You are attempting to retrieve a service using `svcs.Container.get()` when no `svcs.Context` is currently active.
fix
Ensure service retrieval happens within an active `svcs.Context` block, typically `with svcs.Container.get_abstract_context() as container: service = container.get(MyService)`. For framework integrations, use the provided dependency.
Upgrade
Version history
26.2.0latest on PyPI · released Aug 24, 2026
Audit
Dependencies

No dependency data recorded yet.

Agent activity
1 hits · last 30 days
Resources
svcs — pip install svcs · libregistry