> ## Documentation Index
> Fetch the complete documentation index at: https://docs.tokenfactory.nebius.com/llms.txt
> Use this file to discover all available pages before exploring further.

# run-python

Run Python code in an isolated container.

## Description

The `run-python` prompt provides a simple way to execute Python code in a container. It handles image selection and provides clear instructions for the execution.

## Parameters

| Parameter | Type   | Required | Default | Description            |
| --------- | ------ | -------- | ------- | ---------------------- |
| `code`    | string | Yes      | -       | Python code to execute |

## Generated Instructions

When invoked with:

```json theme={null}
{
  "code": "import sys\nprint(f'Python {sys.version}')"
}
```

Returns:

````markdown theme={null}
Run this Python code in a container:

```python
import sys
print(f'Python {sys.version}')
````

Use `run` with `tag:python:3.11-slim` image. If the image doesn’t exist, import it first.

````

## Example Usage

### Simple Calculation

```json
{
  "prompt": "run-python",
  "args": {
    "code": "print(sum(range(100)))"
  }
}
````

### Multi-line Script

```json theme={null}
{
  "prompt": "run-python",
  "args": {
    "code": "def factorial(n):\n    return 1 if n <= 1 else n * factorial(n-1)\n\nfor i in range(10):\n    print(f'{i}! = {factorial(i)}')"
  }
}
```

### With Package Usage

```json theme={null}
{
  "prompt": "run-python",
  "args": {
    "code": "import numpy as np\nprint(np.random.rand(5))"
  }
}
```

Note: If the code requires packages not in the base image, you’ll need to install them first using `install-packages` or `prepare-environment`.

## Implementation Notes

The agent should:

1. Check if `tag:python:3.11-slim` exists with `list_images`
2. If not found, import it with `import_image`
3. Execute the code with `run`:

   ```json theme={null}
   {
     "command": "python -c '<escaped_code>'",
     "image": "tag:python:3.11-slim"
   }
   ```

## See Also

* [run-shell](./run-shell) - Run shell commands
* [sync-and-run](./sync-and-run) - Run with local files
* [install-packages](./install-packages) - Install dependencies first
