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

# Python

> Execute Python code

## Overview

Python tool allows you to execute Python code from your AI agents.

The user sends code; the agent runs it in a sandbox and returns stdout and results.

```mermaid theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
graph LR
    subgraph "PythonTool Flow"
        User[👤 User] --> Agent[🤖 Agent]
        Agent --> Tool[🔧 PythonTool]
        Tool --> Result[✅ Result]
        Result --> Agent
    end

    classDef agent fill:#8B0000,stroke:#7C90A0,color:#fff
    classDef tool fill:#189AB4,stroke:#7C90A0,color:#fff
    classDef output fill:#10B981,stroke:#7C90A0,color:#fff

    class User,Agent agent
    class Tool tool
    class Result output
```

## Installation

```bash theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
pip install "praisonai[tools]"
```

## Quick Start

<Steps>
  <Step title="Simple Usage">
    ```python theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
    from praisonai_tools import PythonTool

    # Initialize
    python = PythonTool()

    # Execute code
    result = python.execute("print(2 + 2)")
    print(result)
    ```
  </Step>

  <Step title="With Configuration">
    Use the same tool with an agent — see **Usage with Agent** below, or pass env vars and options from the sections above.
  </Step>
</Steps>

## Usage with Agent

```python theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
from praisonaiagents import Agent
from praisonai_tools import PythonTool

agent = Agent(
    name="CodeRunner",
    instructions="You help execute Python code.",
    tools=[PythonTool()]
)

response = agent.chat("Calculate the factorial of 10")
print(response)
```

## Available Methods

### execute(code)

Execute Python code.

```python theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
from praisonai_tools import PythonTool

python = PythonTool()
result = python.execute('''
import math
print(math.factorial(10))
''')
```

## Security Warning

⚠️ **Use with caution!** Executing arbitrary code can be dangerous.

## How It Works

```mermaid theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
sequenceDiagram
    participant User
    participant Agent
    participant Tool as Tool
    participant Svc as Python runtime

    User->>Agent: Request
    Agent->>Tool: Call tool
    Tool->>Svc: Query
    Svc-->>Tool: Data
    Tool-->>Agent: Result
    Agent-->>User: Response
```

***

## Best Practices

<AccordionGroup>
  <Accordion title="Sandbox untrusted code">
    Only execute code generated from trusted instructions. Treat all output as untrusted.
  </Accordion>

  <Accordion title="Capture errors">
    Wrap execution so tracebacks are returned to the agent instead of terminating the run.
  </Accordion>

  <Accordion title="Keep runs stateless">
    Pass all inputs explicitly — don't rely on state persisting between executions.
  </Accordion>
</AccordionGroup>

***

## Related Tools

<CardGroup cols={2}>
  <Card title="Shell" icon="book" href="/docs/tools/external/shell">
    Shell commands
  </Card>

  <Card title="Calculator" icon="book" href="/docs/tools/external/calculator">
    Math calculations
  </Card>
</CardGroup>
