Implementation:Bentoml BentoML EnvManager
| Knowledge Sources | |
|---|---|
| Domains | Environment Management, Dependency Injection, Bento Execution |
| Last Updated | 2026-02-13 15:00 GMT |
Overview
Provides a high-level manager class for creating and managing isolated Python environments (currently Conda) for running BentoML bentos.
Description
The EnvManager class acts as a factory and lifecycle manager for isolated Python environments. It supports two modes of operation: ephemeral environments (created in a temporary directory and automatically cleaned up via weakref.finalize) and persistent environments (stored under the BentoML environment store directory). The constructor uses simple_di dependency injection (@inject) to resolve the env_store path from the BentoML container configuration.
The class provides two factory methods: from_bento() creates an EnvManager from an existing Bento object (deriving the environment name from the bento tag for persistent environments), and from_bentofile() is a placeholder for future support of creating environments directly from a bento info specification.
Currently only conda is supported as an environment type; other values raise NotImplementedError.
Usage
Use EnvManager when you need to run a bento inside an isolated conda environment, such as when the bento has conda-specific dependencies that should not be installed in the host Python environment. Use from_bento() for the most common workflow.
Code Reference
Source Location
- Repository: Bentoml_BentoML
- File: src/bentoml/_internal/env_manager/manager.py
- Lines: 1-73
Signature
class EnvManager:
environment: Environment
@inject
def __init__(
self,
env_type: t.Literal["conda"],
bento: Bento,
is_ephemeral: bool = True,
env_name: str | None = None,
env_store: str = Provide[BentoMLContainer.env_store_dir],
): ...
@classmethod
def from_bento(
cls,
env_type: t.Literal["conda"],
bento: Bento,
is_ephemeral: bool = True,
) -> EnvManager: ...
@classmethod
def from_bentofile(
cls, env_type: str, bento_info: BentoInfo, is_ephemeral: str
) -> EnvManager: ...
Import
from bentoml._internal.env_manager.manager import EnvManager
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| env_type | Literal["conda"] | Yes | Type of environment to create (currently only "conda") |
| bento | Bento | Yes | The BentoML Bento object with environment configuration |
| is_ephemeral | bool | No (default True) | Whether the environment is temporary (auto-cleaned) or persistent |
| env_name | str or None | No | Name for the environment; auto-generated if not provided |
| env_store | str | No (injected) | Base directory for persistent environment storage |
Outputs
| Name | Type | Description |
|---|---|---|
| EnvManager | EnvManager | An EnvManager instance with an initialized environment attribute |
| environment | Environment | The underlying Conda (or future) environment instance |
Usage Examples
from bentoml._internal.env_manager.manager import EnvManager
from bentoml._internal.bento.bento import Bento
bento = Bento.from_path("/path/to/bento")
# Create an ephemeral conda environment from a bento
mgr = EnvManager.from_bento(env_type="conda", bento=bento, is_ephemeral=True)
# Run commands in the isolated environment
mgr.environment.run(["python", "main.py"])
# Create a persistent environment (stored under BENTOML_HOME/envs/)
mgr_persistent = EnvManager.from_bento(
env_type="conda", bento=bento, is_ephemeral=False
)