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 CLI Env Manager

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

Overview

Provides the --env CLI option decorator and supporting functions for running BentoML commands within managed environments such as Conda.

Description

This module implements the env_manager decorator that adds a --env option (currently supporting "conda") to BentoML CLI commands. When --env is specified, the decorator intercepts the command execution, resolves the target bento (from a store tag, built bento directory, or project path), creates an Environment object via EnvManager.from_bento, and then re-executes the current CLI command as a subprocess within that environment. The get_environment function handles three cases: (1) a path to a built bento directory (containing bento.yaml), (2) a path to a project directory (not yet supported), or (3) a bento identifier to look up in the BentoStore. The remove_env_arg helper strips the --env argument from sys.argv before re-executing, preventing infinite recursion. A rich.Status spinner is displayed during environment preparation unless debug mode is enabled.

Usage

Use the env_manager decorator on BentoML CLI commands (such as serve) to enable running them within isolated conda environments. The --env conda flag triggers environment creation from the bento's configuration.

Code Reference

Source Location

Signature

def remove_env_arg(cmd_args: list[str]) -> list[str]: ...

@inject
def get_environment(
    bento_identifier: str,
    env: t.Literal["conda"],
    bento_store: BentoStore = Provide[BentoMLContainer.bento_store],
) -> Environment: ...

def env_manager(func: t.Callable[..., t.Any]) -> t.Callable[..., t.Any]: ...

Import

from bentoml_cli.env_manager import env_manager, get_environment, remove_env_arg

I/O Contract

Inputs

Name Type Required Description
bento_identifier str Yes (get_environment) Bento tag string or filesystem path to a bento
env Literal["conda"] Yes (get_environment) The type of environment to create; currently only "conda" is supported
func Callable Yes (env_manager) The Click command function to wrap with the --env option
cmd_args list[str] Yes (remove_env_arg) Command line arguments to strip --env from

Outputs

Name Type Description
Environment Environment The created environment object (get_environment)
Callable Callable The wrapped command function with --env option added (env_manager)
list[str] list[str] Cleaned argument list without --env flag (remove_env_arg)

Usage Examples

# Using the env_manager decorator on a CLI command:
import click
from bentoml_cli.env_manager import env_manager

@click.command()
@env_manager
@click.argument("bento")
def serve(bento: str):
    """Serve a BentoML service."""
    pass

# Command line usage:
# bentoml serve my_bento:latest --env conda
# This creates a conda environment from the bento's config
# and re-runs the serve command inside that environment

# Stripping --env from arguments:
from bentoml_cli.env_manager import remove_env_arg
cleaned = remove_env_arg(["serve", "my_bento", "--env", "conda"])
# cleaned == ["serve", "my_bento"]

Related Pages

Page Connections

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