> ## Documentation Index
> Fetch the complete documentation index at: https://agno-v2-codex-docs-audit-20260719-0149.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Python

> Let an agent write, save, and execute Python code and install packages with PythonTools, including include/exclude tool restrictions.

Build intelligent Agno agents with computing autonomy through several file-system and execution functions that allow:

* Run Python code
* Save to file and run
* `pip install` packages
* List files, read files

PythonTools is a pre-built toolkit you import, unlike the custom [Python Functions as Tools](/examples/tools/python-function-as-tool) pattern.

```python theme={null}

from pathlib import Path

from agno.agent import Agent
from agno.tools.python import PythonTools

# ---------------------------------------------------------------------------
# Create Agent
# ---------------------------------------------------------------------------


# Example 1: All functions available (default behavior)
agent_all = Agent(
    name="Python Agent - All Functions",
    tools=[PythonTools(base_dir=Path("tmp/python"))],
    instructions=["You have access to all Python execution capabilities."],
    markdown=True,
)

# Example 2: Include specific functions only
agent_specific = Agent(
    name="Python Agent - Specific Functions",
    tools=[
        PythonTools(
            base_dir=Path("tmp/python"),
            include_tools=["save_to_file_and_run", "run_python_code"],
        )
    ],
    instructions=["You can only save and run Python code, no package installation."],
    markdown=True,
)

# Example 3: Exclude dangerous functions
agent_safe = Agent(
    name="Python Agent - Safe Mode",
    tools=[
        PythonTools(
            base_dir=Path("tmp/python"),
            exclude_tools=["pip_install_package", "uv_pip_install_package"],
        )
    ],
    instructions=["You can run Python code but cannot install packages."],
    markdown=True,
)

# Use the default agent for examples
agent = agent_all

# ---------------------------------------------------------------------------
# Run Agent
# ---------------------------------------------------------------------------
if __name__ == "__main__":
    agent.print_response(
        "Write a python script for fibonacci series and display the result till the 10th number"
    )
```

<Warning>
  `include_tools` and `exclude_tools` control which toolkit functions are registered. They do not sandbox code executed by `run_python_code` or `save_to_file_and_run`. Executed code runs with the current Python process permissions and can install packages, read environment variables and files, or make network requests. Use an isolated environment with only the credentials and filesystem access the agent needs.
</Warning>

## Run the Example

Set `OPENAI_API_KEY` before running the example.

```bash theme={null}
# Clone and setup repo
git clone --branch v2.7.4 --depth 1 https://github.com/agno-agi/agno.git
cd agno

# Create and activate virtual environment
./scripts/demo_setup.sh
source .venvs/demo/bin/activate

export OPENAI_API_KEY="your_openai_api_key_here"
python cookbook/91_tools/python_tools.py
```

For details, see [PythonTools cookbook](https://github.com/agno-agi/agno/blob/v2.7.4/cookbook/91_tools/python_tools.py).
