Skip to main content

Execution environments

Project description

Exxec

PyPI License Package status Monthly downloads Distribution format Wheel availability Python version Implementation Releases Github Contributors Github Discussions Github Forks Github Issues Github Issues Github Watchers Github Stars Github Repository size Github last commit Github release date Github language count Github commits this month Package status PyUp

Read the documentation!

Basic Usage

Use the get_environment() function to create execution environments:

from exxec import get_environment

# Local execution (same process)
env = get_environment("local")

# Subprocess execution (separate process when executing python code)
env = get_environment("local", isolated=True)

# Docker execution (containerized)
env = get_environment("docker")

# Execute code
async with env:
    result = await env.execute("""
    async def main():
        return "Hello from execution environment!"
    """)
    print(result.result)  # "Hello from execution environment!"

Available Providers

Local Provider

Executes code in the same Python process. Fastest option but offers no isolation.

env = get_environment("local", timeout=30.0)

Parameters:

  • timeout (float): Execution timeout in seconds (default: 30.0)
  • isolated (bool): Whether to execute code in a separate process (default: False)
  • language (Language): Programming language (default: "python")

Docker Provider

Executes code in Docker containers for strong isolation and reproducible environments.

env = get_environment(
    "docker",
    image="python:3.13-slim",
    timeout=60.0,
    language="python"
)

Parameters:

  • lifespan_handler: Tool server context manager (optional)
  • image (str): Docker image to use (default: "python:3.13-slim")
  • timeout (float): Execution timeout in seconds (default: 60.0)
  • language (Language): Programming language (default: "python")

Daytona Provider

Executes code in remote Daytona sandboxes for cloud-based development environments.

env = get_environment(
    "daytona",
    api_url="https://api.daytona.io",
    api_key="your-api-key",
    timeout=300.0,
    keep_alive=False
)

Parameters:

  • api_url (str): Daytona API URL (optional, uses env vars if not provided)
  • api_key (str): API key for authentication (optional)
  • target (str): Target configuration (optional)
  • image (str): Container image (default: "python:3.13-slim")
  • timeout (float): Execution timeout in seconds (default: 300.0)
  • keep_alive (bool): Keep sandbox running after execution (default: False)

E2B Provider

Executes code in E2B sandboxes for secure, ephemeral execution environments.

env = get_environment(
    "e2b",
    template="python",
    timeout=300.0,
    keep_alive=False,
    language="python"
)

Parameters:

  • template (str): E2B template to use (optional)
  • timeout (float): Execution timeout in seconds (default: 300.0)
  • keep_alive (bool): Keep sandbox running after execution (default: False)
  • language (Language): Programming language (default: "python")

Beam Provider

Executes code in Beam cloud sandboxes for scalable, serverless execution environments.

env = get_environment(
    "beam",
    cpu=1.0,
    memory=128,
    keep_warm_seconds=600,
    timeout=300.0,
    language="python"
)

Parameters:

  • cpu (float | str): CPU cores allocated to the container (default: 1.0)
  • memory (int | str): Memory allocated to the container in MiB (default: 128)
  • keep_warm_seconds (int): Seconds to keep sandbox alive, -1 for no timeout (default: 600)
  • timeout (float): Execution timeout in seconds (default: 300.0)
  • language (Language): Programming language (default: "python")

MCP Provider

Executes Python code with Model Context Protocol support for AI integrations.

env = get_environment(
    "mcp",
    dependencies=["requests", "numpy"],
    allow_networking=True,
    timeout=30.0
)

Parameters:

  • dependencies (list[str]): Python packages to install (optional)
  • allow_networking (bool): Allow network access (default: True)
  • timeout (float): Execution timeout in seconds (default: 30.0)

Code Execution Patterns

All providers support two execution patterns:

1. Main Function Pattern

code = """
async def main():
    # Your code here
    return "result"
"""

2. Result Variable Pattern

code = """
import math
_result = math.pi * 2
"""

Error Handling

Execution results include comprehensive error information:

async with env:
    result = await env.execute(code)
    if result.success:
        print(f"Result: {result.result}")
        print(f"Duration: {result.duration:.3f}s")
    else:
        print(f"Error: {result.error}")
        print(f"Error Type: {result.error_type}")

Multi-Language Support

Some providers support multiple programming languages:

# JavaScript execution
env = get_environment("subprocess", language="javascript", executable="node")

# TypeScript execution
env = get_environment("docker", language="typescript", image="node:18")

Advanced Usage

Context Managers

All environments are async context managers for proper resource cleanup:

async with get_environment("docker") as env:
    result1 = await env.execute(code1)
    result2 = await env.execute(code2)  # Reuses same container
# Container automatically cleaned up

Custom Configurations

Each provider supports environment-specific customization:

# Docker with custom image and networking
env = get_environment(
    "docker",
    image="tensorflow/tensorflow:latest-py3",
    timeout=600.0
)

# Subprocess with specific Python version
env = get_environment(
    "subprocess",
    executable="/usr/bin/python3.11",
    timeout=120.0
)

Streaming Output

Some providers support streaming output line by line, useful for long-running processes:

from exxec import get_environment

# Stream output from subprocess execution
env = get_environment("subprocess")

async with env:
    async for line in env.execute_stream("""
    import time
    for i in range(5):
        print(f"Processing step {i+1}...")
        time.sleep(1)
    print("Done!")
    """):
        print(f"Live output: {line}")

# Also works with Docker execution
env = get_environment("docker")
async with env:
    async for line in env.execute_stream(code):
        # Process each line as it's produced
        if "ERROR" in line:
            print(f"⚠️  {line}")
        else:
            print(f"✓ {line}")

Supported providers: docker, local, beam, e2b, modal, vercel, ssh, daytona

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

exxec-0.1.3.tar.gz (68.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

exxec-0.1.3-py3-none-any.whl (94.0 kB view details)

Uploaded Python 3

File details

Details for the file exxec-0.1.3.tar.gz.

File metadata

  • Download URL: exxec-0.1.3.tar.gz
  • Upload date:
  • Size: 68.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.16 {"installer":{"name":"uv","version":"0.9.16","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"25.10","id":"questing","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for exxec-0.1.3.tar.gz
Algorithm Hash digest
SHA256 eb84d7255a405faaadd7241d7e2c9e11451e5a0e1cc3a5ba14c64f0e83430b84
MD5 d5c083a6c343383cd9bf69628fccaa40
BLAKE2b-256 690298f9487943983b6a94d78458362dc5b74bd1dfd3ee2edf476c2c3548e37b

See more details on using hashes here.

File details

Details for the file exxec-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: exxec-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 94.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.16 {"installer":{"name":"uv","version":"0.9.16","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"25.10","id":"questing","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for exxec-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 08f3dba9b51a5a278305aa683d0a7b79761a40878631db4c81fc2479cbad35f4
MD5 7bc033003c01034b47e9817f9f1f0445
BLAKE2b-256 e4d21df0e3e8629d8abbbee7f4af79fcd0f672874d5fc9eedcb9b22e8c507f37

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page