Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Bentoml BentoML Environment Types

From Leeroopedia
Knowledge Sources
Domains Environment Management, Conda, Dependency Installation
Last Updated 2026-02-13 15:00 GMT

Overview

Defines the abstract Environment base class and the Conda concrete implementation for creating, managing, and running commands within isolated Python environments for BentoML bentos.

Description

The Environment abstract class provides a common interface for managing isolated Python environments. It is defined as an attrs dataclass with fields for the environment name, filesystem path, and associated Bento object. On initialization (__attrs_post_init__), it resolves the executable path and creates the environment if the path does not exist. Subclasses must implement get_executable(), create(), and run() methods. A static helper run_script_subprocess() executes shell scripts via bash with optional debug flags (-euxo pipefail) and raises BentoMLException on non-zero exit codes.

The Conda subclass locates the conda executable via the CONDA_EXE environment variable and creates a conda environment under the bento's environment store. The create() method reads the Python version from the bento, creates a conda env with that Python version, optionally applies a conda environment.yml file, and runs the bento's install.sh script. The run() method activates the conda environment and runs the given commands within it.

Usage

Use Conda (or future environment types) when you need to run a bento in an isolated environment with specific conda dependencies. Typically instantiated through the EnvManager class rather than directly.

Code Reference

Source Location

Signature

@attr.define
class Environment(ABC):
    name: str
    path: Path
    bento: Bento
    env_exe: str  # set in __attrs_post_init__

    def get_executable(self) -> str: ...      # abstract
    def create(self): ...                      # abstract
    def run(self, commands: list[str]): ...    # abstract

    @staticmethod
    def run_script_subprocess(
        script_file_path: t.Union[str, os.PathLike[str]],
        capture_output: bool,
        debug_mode: bool,
    ): ...

class Conda(Environment):
    def get_executable(self) -> str: ...
    def create(self): ...
    def run(self, commands: list[str]): ...

Import

from bentoml._internal.env_manager.envs import Environment
from bentoml._internal.env_manager.envs import Conda

I/O Contract

Inputs

Name Type Required Description
name str Yes Name for the environment instance
path Path Yes Filesystem path where the environment will be stored
bento Bento Yes The BentoML Bento object containing environment configuration files
commands list[str] Yes (for run) List of shell commands to execute in the activated environment

Outputs

Name Type Description
env_exe str Path to the environment executable (e.g., conda binary)
(side effect) None create() sets up the environment on disk; run() executes commands in the environment

Usage Examples

from bentoml._internal.env_manager.envs import Conda
from bentoml._internal.bento.bento import Bento

# Typically created via EnvManager, but can be used directly:
bento = Bento.from_path("/path/to/bento")
conda_env = Conda(name="my_env", path="/path/to/envs", bento=bento)

# The environment is automatically created on initialization if it does not exist.

# Run commands inside the activated conda environment
conda_env.run(["python", "-c", "import torch; print(torch.__version__)"])

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment