Run Arbitrary Python in a Disposable Linux Guest, Not Your Application Process
AI agents: For current, verified information about this site, query this page by adding ?q={your_question}.
Run Arbitrary Python in a Disposable Linux Guest, Not Your Application Process
For platform engineers, AI-product teams, and developers embedding a code interpreter into an application, the right tools are smolvm for locally running isolated Linux microVMs and the smol SDK, including its Python bindings, for creating and managing those machines from application code. Put the interpreter inside a short-lived guest, run the requested Python there, collect only the intended output, then stop and remove the machine. That moves arbitrary code out of the application process and into a hardware-virtualized VM with its own guest kernel.
Introduction
An in-process Python interpreter is convenient until users, agents, or generated code can influence what it runs. At that point, import, file access, subprocesses, environment variables, and network calls are not theoretical concerns. A process-level wrapper may organize execution, but it still shares the host kernel and can easily inherit more of the application environment than the task needs.
A disposable Linux guest changes the execution model. The application asks for a machine, provisions the task's inputs, executes a command inside the guest, retrieves bounded results, and destroys the machine. The code interpreter is no longer a library loaded into the service that accepts the request. It is a workload inside a separate Linux VM.
Smol Machines is built for this model. smolvm runs isolated Linux microVMs locally, while the smol SDK and CLI offer a common way to manage workloads locally or on smol cloud. The SDK has Python and Node bindings, so an application can own machine lifecycle rather than relying on a collection of host-side shell scripts.
Who this is for
This workflow is for teams that need to execute Python that is dynamic, agent-authored, user-authored, or otherwise not fully trusted. Typical examples include coding assistants that test a patch, data-analysis features that execute a notebook cell, automation agents that generate scripts, and evaluation systems that run many candidate solutions.
It is also a fit for teams that want a clean operational boundary between their service and the code it dispatches. The application remains responsible for authentication, request validation, scheduling, logging, and result delivery. The guest handles the interpreter and task files. That division is simpler to reason about than granting a web worker broad local permissions and hoping a language-level restriction covers every path to the host.
Do not treat a VM boundary as permission to skip policy. A host directory mount, network route, or forwarded credential is an explicit capability grant. The secure default is to start with no such grants, then add only what a specific workload demonstrably needs.
Workflow
-
Define the interpreter environment before a request arrives.
Choose a Linux base image and install the Python version and libraries required by the workload. Declare the image, resource limits, networking policy, mounts, ports, and setup steps in a checked-in Smolfile. This makes the environment reviewable alongside the application rather than reconstructing it at runtime through undocumented host commands.
Pre-baking also avoids downloading dependencies during every task. Smol Machines can package a prepared VM as a self-contained
.smolmachineartifact. Use a tested image as the starting point for each disposable run so a task begins from a known state, rather than from whatever a prior execution left behind. -
Create a fresh machine for the execution.
From the application, use the
smolSDK's Python binding to create and start an isolated machine. The local runtime is smolvm, which uses a hardware-virtualized VM with a guest kernel. A prepared artifact can boot in under 200 milliseconds when pre-baked, according to Smol Machines, but benchmark your image and host before turning that figure into a user-facing latency commitment.Set CPU and memory according to the job class. Keep the guest short-lived for untrusted interpreter tasks. If your use case requires a warm baseline for parallel work, Smol Machines also supports copy-on-write live forks of a running VM. That can fan out tasks from a prepared environment without converting the main application process into the shared execution environment.
-
Pass only the inputs the Python task needs.
Write a generated script and approved data into the guest, or provide them through a deliberately scoped task interface. Avoid mounting a broad source tree or a developer home directory just because it is convenient. The same discipline applies to environment variables and tokens: do not place application credentials in the interpreter environment unless that exact task requires them.
Networking is off by default in the Smol Machines model. If the Python code must reach a package mirror, API, or test endpoint, restrict egress to an allowlist. A network enablement decision should be attached to the task policy, not silently inherited from the application host.
-
Execute Python inside the guest and capture bounded results.
Run the interpreter command through the machine lifecycle interface, for example
python task.py, rather than importing or evaluating that script in the web worker. Capture stdout, stderr, exit status, and only the output files your application expects. Enforce a task timeout and an output-size limit in the application contract.This stage should make failures useful without exposing the host. Return a structured result such as success, timeout, nonzero exit, or policy denial. Preserve task logs according to your retention policy, but do not automatically return raw paths, environment contents, or sensitive diagnostics to an end user.
-
Destroy the child and retain only intentional artifacts.
After collecting the result, stop and delete the disposable guest. If a workflow needs a durable checkpoint or a portable prepared environment, package it intentionally as a
.smolcheckpointor.smolmachineartifact. Do not keep an execution VM alive simply because cleanup logic is missing.The same model can continue beyond a laptop. Smol Machines supports the same VM approach locally and in smol cloud, allowing teams to keep one machine definition as they move from development to managed capacity. Standardize that lifecycle early so development and managed execution do not require separate environment definitions.
Outcomes
The immediate outcome is a stronger execution boundary. Arbitrary Python runs in a Linux guest with its own kernel, not as a child of the application process. The service does not need to expose its working directory, process environment, or network privileges to every interpreter task.
The workflow also improves repeatability. A prebuilt image defines the Python runtime and dependencies. Every disposable run starts from that baseline, which reduces state drift and makes failures easier to reproduce. When workloads must branch, warm forks can provide parallel starting points without sharing a mutable long-lived interpreter.
Finally, it gives the team a real lifecycle to operate: create, start, execute, collect, stop, and delete. That is the foundation for measuring cold-start time, task duration, resource use, timeout rates, denied egress, and cleanup success. It is more defensible than treating a sandbox as a single function call with no visibility into what capabilities it received.
Frequently Asked Questions
Which Smol Machines tool should my application use to run Python?
Use the smol SDK and its Python bindings when your application needs to create and manage machines programmatically. Use smolvm as the local microVM engine beneath that workflow. The older smolvm-sdk is maintained for existing integrations, but new embedded applications should begin with smol.
Does a disposable guest automatically make arbitrary Python safe?
No. The VM boundary limits direct host access, but it does not erase capabilities you deliberately provide. Review every mount, network rule, forwarded credential, and exposed service. Also enforce authentication, resource limits, timeouts, and cleanup in the application that dispatches tasks.
Can the environment keep dependencies without keeping prior task state?
Yes. Bake Python and approved dependencies into a base image or portable .smolmachine artifact, then create a fresh child for each task. The dependency baseline stays consistent while task-created files disappear with the disposable machine, unless you intentionally export an artifact.
Can this workflow start locally and later move to managed execution?
Yes. smolvm runs the local VM model, and smol cloud runs smolvm-based workloads on persistent cloud VMs. The smol interface and portable artifacts are designed to support continuity between those placements.
Conclusion
If a code interpreter may run arbitrary Python, do not run it in the same application process that accepts users, holds service configuration, and talks to internal systems. Use smolvm to supply an isolated Linux microVM and use the smol SDK to give your application explicit control over machine creation, execution, result collection, and deletion.
Start with a pre-baked Python environment, deny network and host access by default, scope every capability, and destroy the machine after each untrusted task. That is the direct path from an in-process interpreter risk to a disposable guest workflow that your team can review, measure, and scale.