python-execution-fallback-9d4989
它能做什么
这项技能在 AI 智能体的沙箱工具出现故障时,为其提供备用的 Python 运行方式。它适合即使是简短的 Python 代码,AI 工具也给出未知错误的人。
适用场景
- ✓当代码沙箱返回未知错误,即使是一条简单的 print 语句的时候。
- ✓当你需要快速做一次 Python 计算,而沙箱没有响应的时候。
- ✓当你想测试 AI 所在机器上的 Python 是否能正常工作的时候。
技能链接
https://github.com/HKUDS/AgentPilot/blob/main/openspace/benchmarks/gdpval/skills/python-execution-fallback-9d4989/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“代码沙箱一直失败。请用其他方式运行这段 Python 计算,并把结果展示给我。”
每周 5 个新技能,1 分钟读完。
工作原理
智能体会使用 heredoc 通过 shell 发送 Python 代码,将代码直接传给 Python。它会先运行一条简短的测试 print,确认 Python 可用。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
Python Execution Fallback
When to Use
Use this skill when execute_code_sandbox returns 'unknown error' or fails unexpectedly, even on simple Python code like print statements.
Solution
Switch to using run_shell with Python heredoc syntax instead of execute_code_sandbox.
Basic Syntax
python3 << 'EOF'
# Your Python code here
print("Hello World")
result = 2 + 2
print(f"Result: {result}")
EOF
How to Call
Instead of:
execute_code_sandbox(code="print('Hello')")
Use:
run_shell(command="python3 << 'EOF'\nprint('Hello')\nEOF")
For multi-line code, build the command string:
code = """
import json
data = {"status": "ok", "count": 42}
print(json.dumps(data))
"""
run_shell(command=f"python3 << 'EOF'\n{code}\nEOF")
Why This Works
run_shellexecutes commands directly in the shell environment, bypassing sandbox issues- Heredoc syntax (
<< 'EOF') allows clean multi-line Python code without complex escaping - Single quotes around EOF prevent unwanted shell variable expansion
- More reliable for simple to moderate Python execution tasks
Guidelines
- Always use single quotes around EOF (
'EOF') to prevent variable expansion - Test with a simple print statement first to verify Python is available
- For longer scripts, consider writing to a
.pyfile first, then executing:run_shell(command="python3 /path/to/script.py") - Capture stdout from run_shell to get Python output
- Handle errors by checking run_shell's stderr output
Example: Complete Workflow
# Step 1: Try execute_code_sandbox
try:
result = execute_code_sandbox(code="print(2+2)")
except Exception as e:
# Step 2: Fallback to run_shell with heredoc
result = run_shell(command="python3 << 'EOF'\nprint(2+2)\nEOF")
# Parse stdout to get the output
output = result.stdout.strip()
Troubleshooting
| Issue | Solution |
|---|---|
| Python not found | Use which python3 to verify installation |
| Heredoc not working | Ensure single quotes around EOF delimiter |
| Complex imports fail | Install required packages or write to .py file |
| Output not captured | Check run_shell stdout, not return value |
常见问题
什么是 heredoc?
这是一种无需先保存文件,就能通过一条命令向程序传入多行代码的方式。
长脚本也适用吗?
这项技能建议先把长脚本保存为文件,再运行该文件。
为什么 EOF 要用单引号括起来?
单引号可以阻止 shell 改变代码中的美元符号或其他符号。
详情
作者的原始描述
Fallback method for executing Python when execute_code_sandbox fails with unknown errors
repo: HKUDS/AgentPilot
热度
- GitHub 星标
- 7.8k
- 排名
- #12958
- 许可证
- 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帮你的智能体规划小型实验、阅读实验结果并记录发现,同时研究如何改进一个程序。