{"id":2298,"library":"svcs","title":"svcs: A Flexible Service Locator","description":"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.","status":"active","version":"25.1.0","language":"en","source_language":"en","source_url":"https://github.com/hynek/svcs","tags":["service locator","dependency injection","resource management","framework integration"],"install":[{"cmd":"pip install svcs","lang":"bash","label":"Install core library"}],"dependencies":[],"imports":[{"symbol":"Registry","correct":"from svcs import Registry"},{"symbol":"Container","correct":"from svcs import Container"},{"note":"Commonly used for framework integrations or when explicit registration of a callable factory is needed.","symbol":"svcs_from_factory","correct":"from svcs import svcs_from_factory"}],"quickstart":{"code":"import svcs\n\nclass MyService:\n    def __init__(self, name: str):\n        self.name = name\n\n    def hello(self) -> str:\n        return f\"Hello from {self.name}!\"\n\ndef create_my_service() -> MyService:\n    return MyService(name=\"quickstart_service\")\n\n# 1. Create a registry and register your service factory\nreg = svcs.Registry()\nreg.register_factory(MyService, create_my_service)\n\n# 2. Get a container, ensuring it's closed properly\nwith svcs.Container(reg) as container:\n    # 3. Acquire the service\n    service = container.get(MyService)\n    print(service.hello())\n","lang":"python","description":"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."},"warnings":[{"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.","message":"The 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.","severity":"breaking","affected_versions":">=23.21.0"},{"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.","message":"Failing to close `svcs.Container` or `svcs.Registry` instances with pending cleanups will raise a `ResourceWarning`.","severity":"gotcha","affected_versions":">=23.15.0"}],"env_vars":null,"last_verified":"2026-04-09T00:00:00.000Z","next_check":"2026-07-08T00:00:00.000Z"}