Skip to content

PythonEnvironment

autogen.environments.python_environment.PythonEnvironment #

PythonEnvironment()

Bases: ABC

Python execution environments base class

Initialize the Python environment.

Source code in autogen/environments/python_environment.py
def __init__(self):
    """
    Initialize the Python environment.
    """
    self._token = None

    # Set up the environment
    self._setup_environment()

get_executable abstractmethod #

get_executable()

Get the path to the Python executable in this environment.

RETURNS DESCRIPTION
str

The full path to the Python executable.

Source code in autogen/environments/python_environment.py
@abstractmethod
def get_executable(self) -> str:
    """
    Get the path to the Python executable in this environment.

    Returns:
        The full path to the Python executable.
    """
    pass

execute_code abstractmethod async #

execute_code(code, script_path, timeout=30)

Execute the given code in this environment.

PARAMETER DESCRIPTION
code

The Python code to execute.

TYPE: str

script_path

Path where the code should be saved before execution.

TYPE: str

timeout

Maximum execution time in seconds.

TYPE: int DEFAULT: 30

RETURNS DESCRIPTION
dict[str, Any]

dict with execution results including stdout, stderr, and success status.

Source code in autogen/environments/python_environment.py
@abstractmethod
async def execute_code(self, code: str, script_path: str, timeout: int = 30) -> dict[str, Any]:
    """
    Execute the given code in this environment.

    Args:
        code: The Python code to execute.
        script_path: Path where the code should be saved before execution.
        timeout: Maximum execution time in seconds.

    Returns:
        dict with execution results including stdout, stderr, and success status.
    """
    pass

get_current_python_environment classmethod #

get_current_python_environment(python_environment=None)

Get the current Python environment or the specified one if provided.

PARAMETER DESCRIPTION
python_environment

Optional environment to return if specified.

TYPE: Optional[PythonEnvironment] DEFAULT: None

RETURNS DESCRIPTION
Optional[PythonEnvironment]

The current Python environment or None if none is active.

Source code in autogen/environments/python_environment.py
@classmethod
def get_current_python_environment(
    cls, python_environment: Optional["PythonEnvironment"] = None
) -> Optional["PythonEnvironment"]:
    """
    Get the current Python environment or the specified one if provided.

    Args:
        python_environment: Optional environment to return if specified.

    Returns:
        The current Python environment or None if none is active.
    """
    if python_environment is not None:
        return python_environment
    try:
        return cls._current_python_environment.get()
    except LookupError:
        return None