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

# Python

**PythonTools** 使 Agent 能够编写和运行 Python 代码。

## 示例

下面的 Agent 将编写一个创建斐波那契数列的 Python 脚本，将其保存到文件，运行它并返回结果。

```python cookbook/tools/python_tools.py theme={null}
from agno.agent import Agent
from agno.tools.python import PythonTools

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

## Toolkit 参数

| 参数             | 类型     | 默认值     | 描述                                 |
| -------------- | ------ | ------- | ---------------------------------- |
| `base_dir`     | `Path` | `None`  | 指定操作的基目录。默认为 `None`，表示当前工作目录。      |
| `save_and_run` | `bool` | `True`  | 如果为 `True`，则保存并运行代码。适用于保存后执行脚本。    |
| `pip_install`  | `bool` | `False` | 在运行代码之前启用必需包的 pip 安装。              |
| `run_code`     | `bool` | `False` | 确定是否应执行代码。                         |
| `list_files`   | `bool` | `False` | 如果为 `True`，则列出指定基目录中的所有文件。         |
| `run_files`    | `bool` | `False` | 如果为 `True`，则运行在指定目录中找到的 Python 文件。 |
| `read_files`   | `bool` | `False` | 如果为 `True`，则读取指定目录中文件的内容。          |
| `safe_globals` | `dict` | -       | 指定一组全局变量，这些变量在执行期间被认为是安全的。         |
| `safe_locals`  | `dict` | -       | 指定一组局部变量，这些变量在执行期间被认为是安全的。         |

## Toolkit 函数

| 函数                                | 描述                                                                                                                                   |
| --------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------ |
| `save_to_file_and_run`            | 此函数将 Python 代码保存到名为 `file_name` 的文件中，然后运行它。如果成功，则返回 `variable_to_return` 的值（如果已提供），否则返回成功消息。如果失败，则返回错误消息。请确保 `file_name` 以 `.py` 结尾。 |
| `run_python_file_return_variable` | 此函数运行 Python 文件中的代码。如果成功，则返回 `variable_to_return` 的值（如果已提供），否则返回成功消息。如果失败，则返回错误消息。                                                   |
| `read_file`                       | 读取 `file_name` 文件的内容，成功时返回文件内容。                                                                                                      |
| `list_files`                      | 返回基目录中的文件列表。                                                                                                                         |
| `run_python_code`                 | 此函数在当前环境中运行 Python 代码。如果成功，则返回 `variable_to_return` 的值（如果已提供），否则返回成功消息。如果失败，则返回错误消息。                                                 |
| `pip_install_package`             | 此函数使用 pip 在当前环境中安装一个包。如果成功，则返回成功消息。如果失败，则返回错误消息。                                                                                     |

## 开发者资源

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