# Python (/tools/toolkits/local/python)



**PythonTools** enable an Agent to write and run python code.

## Prerequisites [#prerequisites]

[Create and activate a virtual environment](/sdk/setup), then install:

```shell
uv pip install agno openai
```

The Agent model uses an OpenAI key, separately from any toolkit provider credentials.

## Set OpenAI Key [#set-openai-key]

Set your `OPENAI_API_KEY` as an environment variable. You can get one [from OpenAI](https://platform.openai.com/account/api-keys).

<CodeBlockTabs defaultValue="Mac">
  <CodeBlockTabsList>
    <CodeBlockTabsTrigger value="Mac">
      Mac
    </CodeBlockTabsTrigger>

    <CodeBlockTabsTrigger value="Windows PowerShell">
      Windows PowerShell
    </CodeBlockTabsTrigger>
  </CodeBlockTabsList>

  <CodeBlockTab value="Mac">
    ```bash
    export OPENAI_API_KEY=sk-***
    ```
  </CodeBlockTab>

  <CodeBlockTab value="Windows PowerShell">
    ```powershell
    $env:OPENAI_API_KEY = "your-openai-api-key"
    ```
  </CodeBlockTab>
</CodeBlockTabs>

## Example [#example]

The following agent will write a python script that creates the fibonacci series, save it to a file, run it and return the result.

```python title="cookbook/91_tools/python_tools.py"
from agno.agent import Agent
from agno.tools.python import PythonTools

agent = Agent(tools=[PythonTools()])
agent.print_response("Write a python script for fibonacci series and display the result till the 10th number")
```

<Warning>
  `PythonTools` can run arbitrary Python code and install packages with no command restriction. Keep human supervision for untrusted prompts, and run inside a sandbox (container, VM, or [Daytona](/tools/toolkits/others/daytona)) for untrusted code execution.
</Warning>

## Toolkit Params [#toolkit-params]

| Parameter              | Type   | Default | Description                                                                                            |
| ---------------------- | ------ | ------- | ------------------------------------------------------------------------------------------------------ |
| `base_dir`             | `Path` | `None`  | Specifies the base directory for operations. Default is None, indicating the current working directory |
| `safe_globals`         | `dict` | `None`  | Dictionary of global variables that are considered safe to use during execution                        |
| `safe_locals`          | `dict` | `None`  | Dictionary of local variables that are considered safe to use during execution                         |
| `restrict_to_base_dir` | `bool` | `True`  | If True, file operations cannot escape `base_dir`                                                      |

## Toolkit Functions [#toolkit-functions]

| Function                          | Description                                                                                                                                                                                                                                                            |
| --------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `save_to_file_and_run`            | This function saves Python code to a file called `file_name` and then runs it. If successful, returns the value of `variable_to_return` if provided otherwise returns a success message. If failed, returns an error message. Make sure the file\_name ends with `.py` |
| `run_python_file_return_variable` | This function runs code in a Python file. If successful, returns the value of `variable_to_return` if provided otherwise returns a success message. If failed, returns an error message.                                                                               |
| `read_file`                       | Reads the contents of the file `file_name` and returns the contents if successful.                                                                                                                                                                                     |
| `list_files`                      | Returns a list of files in the base directory                                                                                                                                                                                                                          |
| `run_python_code`                 | This function runs Python code in the current environment. If successful, returns the value of `variable_to_return` if provided otherwise returns a success message. If failed, returns an error message.                                                              |
| `pip_install_package`             | This function installs a package using pip in the current environment. If successful, returns a success message. If failed, returns an error message.                                                                                                                  |
| `uv_pip_install_package`          | This function installs a package using uv and pip in the current environment. If successful, returns a success message. If failed, returns an error message.                                                                                                           |

You can use `include_tools` or `exclude_tools` to modify the list of tools the agent has access to. Learn more about [selecting tools](/tools/selecting-tools).

## Developer Resources [#developer-resources]

* [Tools](https://github.com/agno-agi/agno/blob/main/libs/agno/agno/tools/python.py)
