code-exec-fallback
它能做什么
这项技能是在内置运行器反复失败时,运行 Python 代码的备用方法。它适合需要让 AI 的数据或计算脚本无论如何都能运行的人。
适用场景
- ✓当运行器因为限制、超时或缺少软件包而失败两次或以上的时候。
- ✓当我需要在设定的时间限制内,从 shell 运行数据脚本的时候。
- ✓当我希望 AI 读取运行输出并报告任何错误的时候。
技能链接
https://github.com/HKUDS/AgentPilot/blob/main/openspace/benchmarks/gdpval/skills/code-exec-fallback/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“Python 运行器在我的电子表格上一直超时。你能把脚本保存为文件,改为运行它吗?”
每周 5 个新技能,1 分钟读完。
工作原理
AI 会把脚本写入文件,在 shell 中用 python3 运行,在设定的时间内等待,并读取输出。之后可以删除该文件。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
Code Execution Fallback
When to Use
Use this pattern when execute_code_sandbox fails repeatedly (typically 2+ attempts) due to environment limitations, timeouts, dependency issues, or sandbox restrictions.
The Pattern
Instead of executing code directly in the sandbox, write the Python script to a file and execute it via shell:
- Write the script using
write_file - Execute via shell using
run_shellwithpython3 script.py - Clean up (optional) remove the temporary file
Step-by-Step Instructions
Step 1: Write the Python Script
Use write_file to save your Python code:
- Path: Choose a descriptive name (e.g., "process_data.py", "analyze.py")
- Content: Your complete Python script with all imports and logic
Step 2: Execute via Shell
Use run_shell to execute:
- Command: "python3 <script_name>.py"
- Timeout: Set appropriately for your task (default 30s, increase if needed)
Step 3: Handle Output
- Capture stdout/stderr from run_shell
- Parse results as needed
- Optionally delete the script file after execution
Example
# Instead of this (which may fail):
execute_code_sandbox(code="import pandas as pd; df = pd.read_csv('data.csv')...")
# Do this:
write_file(path="analyze.py", content="""
import pandas as pd
import json
df = pd.read_csv('data.csv')
result = df.groupby('category').sum()
print(json.dumps(result.to_dict()))
""")
run_shell(command="python3 analyze.py", timeout=60)
Tips for Success
- Include all imports in the script file - the shell environment may differ from the sandbox
- Use absolute paths or ensure working directory is correct
- Add error handling to your script for better debugging
- Increase timeout for long-running operations (default is 30s)
- Print structured output (JSON) if you need to parse results
- Clean up temporary files after successful execution to avoid clutter
When This Helps
- Sandbox has missing dependencies
- Code execution times out in sandbox but would work in shell
- File I/O operations are restricted in sandbox
- Need to run external commands or system utilities
- Complex multi-file projects that need proper file structure
常见问题
为什么用文件,而不用运行器?
指南说,在某些失败情况下,这种方式已被证明更可靠。
默认时间限制是多少?
默认为 30 秒,任务需要更长时间时可以调高。
脚本需要特殊设置吗?
脚本应包含所有导入和逻辑,使其能够独立运行。
详情
作者的原始描述
Fallback pattern for executing Python code when execute_code_sandbox fails
repo: HKUDS/AgentPilot
热度
- GitHub 星标
- 7.8k
- 排名
- #12845
- 许可证
- MIT
- 更新时间
- 2026/11/1
评论
- …
同一仓库的更多技能
agentpilot
7.8k帮你的智能体准备任务并运行 AgentPilot 循环,逐步改进项目:循环会提出修改、测试修改并为其打分。
openspace
7.8k帮你的智能体设置 OpenSpace,这是一个用于存储和获取智能体技能的工具,并通过 MCP 将其连接到你的 AI 应用。
rsi-arena
7.8k帮你的智能体设置并运行标准 AI 基准测试,例如 AIME 和 Terminal-Bench,用来比较不同的改进方法。
experiment-guide
7.8k帮你的智能体规划小型实验、阅读实验结果并记录发现,同时研究如何改进一个程序。