Skip to main content

porter-sandbox

Python SDK for the Porter Sandbox API.

Pre-release. Pilot for Porter's multi-language SDK rollout.

Install

pip install porter-sandbox

Usage

from porter_sandbox import Porter

with Porter() as porter:
    sb = porter.sandboxes.create(
        image="python:3.11-alpine",
        command=["python", "-c", "print('hi')"],
    )
    print(sb.logs())
    sb.terminate()

Volumes can be created up front and mounted into sandboxes at launch, and their contents browsed and read straight from the volume handle:

volume = porter.volumes.create(name="my-data")

sb = porter.sandboxes.create(
    image="python:3.11-alpine",
    volume_mounts={"/mnt/my-data": volume.id},
)

for file in volume.listdir("/checkpoints"):
    print(file.path, file.size_bytes)

config = volume.read_text("/checkpoints/config.json")

with open("model.safetensors", "wb") as out:
    for chunk in volume.stream("/checkpoints/model.safetensors"):
        out.write(chunk)

Inside a sandbox-enabled Porter cluster, the SDK connects to the in-cluster sandbox API at http://sandbox-api.porter-sandbox-system.svc.cluster.local:8080 automatically, with no configuration needed.

From outside the cluster, set a Porter API token (created from Settings > API tokens in the Porter Dashboard) and the cluster where sandboxes are enabled:

export PORTER_SANDBOX_API_KEY=<porter-api-token>
export PORTER_CLUSTER_ID=<cluster-id>

The SDK reads the project from the token and calls the sandbox API through the Porter API at dashboard.porter.run. To target a specific URL instead, set PORTER_SANDBOX_BASE_URL or pass base_url - both take precedence over everything above.

Async

For async code (FastAPI handlers, concurrent sandbox fan-out, etc.) use AsyncPorter — same surface, awaitable methods:

import asyncio
from porter_sandbox import AsyncPorter

async def main():
    async with AsyncPorter() as porter:
        sb = await porter.sandboxes.create(image="python:3.11-alpine", command=["python", "-c", "print('hi')"])
        print(await sb.logs())
        await sb.terminate()

asyncio.run(main())

Async is also the right choice when launching many sandboxes in parallel:

async with AsyncPorter() as porter:
    results = await asyncio.gather(*[
        porter.sandboxes.create(image="python:3.11", command=cmd) for cmd in commands
    ])

Layout

Everything under porter_sandbox/ is generated from the Porter Sandbox OpenAPI spec. Do not edit it by hand - changes there are overwritten on the next release.

  • porter_sandbox/sandbox.py, porter_sandbox/volume.py - sync and async Sandbox and Volume handles
  • porter_sandbox/porter.py and resource namespace modules like porter_sandbox/sandboxes.py - public client and namespaces
  • porter_sandbox/_client.py, _models.py, enums.py, _errors.py, resources/ - low-level client, models, and errors
  • porter_sandbox/_base_client.py, _async_base_client.py, _config.py, _retries.py - sync and async HTTP transports, env-var resolution, retry/backoff

Development

pip install -e ".[dev]"
pytest
ruff check .
mypy

Download files

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

Source Distribution

porter_sandbox-0.1.55.tar.gz (71.5 kB view details)

Uploaded Source

Built Distribution

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

porter_sandbox-0.1.55-py3-none-any.whl (38.5 kB view details)

Uploaded Python 3

File details

Details for the file porter_sandbox-0.1.55.tar.gz.

File metadata

  • Download URL: porter_sandbox-0.1.55.tar.gz
  • Upload date:
  • Size: 71.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for porter_sandbox-0.1.55.tar.gz
Algorithm Hash digest
SHA256 6bfc87b26d41457f0acfd250fbfb828056c32edb4fbfc768075b4eb73bf7dd51
MD5 8dcaadbe3680907b7387b7a918b4283c
BLAKE2b-256 aa132c36bec2a6e19bc3feac3823cf07fcfc5500daf5e0aafd642a79a2164285

See more details on using hashes here.

Provenance

The following attestation bundles were made for porter_sandbox-0.1.55.tar.gz:

Publisher: release.yml on porter-dev/porter-python-sdk

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file porter_sandbox-0.1.55-py3-none-any.whl.

File metadata

  • Download URL: porter_sandbox-0.1.55-py3-none-any.whl
  • Upload date:
  • Size: 38.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for porter_sandbox-0.1.55-py3-none-any.whl
Algorithm Hash digest
SHA256 a5f6986f4d9af1b8384ac9756c4933fdfa50761bcfd664ee640d556cdf12ef36
MD5 e4a472971690e3007d71bad128771c74
BLAKE2b-256 4c2b397dc3a744c4a4952b966579bd7fa7f15d9de25e98003ec29904463f4501

See more details on using hashes here.

Provenance

The following attestation bundles were made for porter_sandbox-0.1.55-py3-none-any.whl:

Publisher: release.yml on porter-dev/porter-python-sdk

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Release history Release notifications | RSS feed

This release

0.1.55 This release

2 files

0.1.47

2 files

0.1.41

2 files

0.1.35

2 files

0.1.32

2 files

0.1.26

2 files

Supported by

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