shell-python-heredoc
它能做什么
这项技能展示当沙箱持续失败时,AI 智能体如何通过 shell 运行多行 Python 脚本。它适合脚本需要多个导入语句或需要清晰错误输出的人。
适用场景
- ✓当代码沙箱因无法解释的错误失败两次或以上的时候。
- ✓当脚本包含多个导入,例如 reportlab 或 pandas,且太长而无法放在一行中的时候。
- ✓当你需要看到 stdout 和 stderr,以便查明问题所在的时候。
技能链接
https://github.com/HKUDS/AgentPilot/blob/main/openspace/benchmarks/gdpval/skills/shell-python-heredoc/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“因为沙箱失败了两次,请通过 shell 运行我的多行 ReportLab 脚本。把输出展示给我。”
每周 5 个新技能,1 分钟读完。
工作原理
智能体会把脚本包裹在一个带有引号结束标记的 heredoc 中,并用 python3 运行,同时设置时间限制。然后它读取 stdout 和 stderr,以判断任务是否成功。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
Shell Python Heredoc (Fallback Pattern)
Overview
When execute_code_sandbox fails repeatedly (e.g., with "unknown error"), use run_shell with a Python heredoc as a reliable fallback for executing multi-line Python scripts with imports.
When to Use
execute_code_sandboxhas failed 2+ times with unexplained errors- You need to run Python code with multiple imports (e.g., ReportLab, pandas)
- The script is too complex for a one-liner but doesn't require persistence between calls
- You need better error visibility from raw stdout/stderr
How to Use
Basic Pattern
run_shell(
command="""python3 << 'EOF'
import sys
# Your Python code here
print("Hello from heredoc")
EOF""",
timeout=300
)
Key Syntax Rules
- Use
<< 'EOF'(with single quotes) to prevent shell variable expansion - Indent Python code relative to the heredoc delimiter
- End with
EOFon its own line (no leading whitespace) - Capture output from stdout/stderr for debugging
Example: Generating a PDF with ReportLab
run_shell(
command="""python3 << 'EOF'
from reportlab.lib.pagesizes import letter
from reportlab.pdfgen import canvas
c = canvas.Canvas("output.pdf", pagesize=letter)
c.drawString(100, 750, "Hello PDF")
c.save()
print("PDF created successfully")
EOF""",
timeout=300
)
Example: Multi-file Data Processing
run_shell(
command="""python3 << 'EOF'
import pandas as pd
import json
# Load and process data
df = pd.read_csv("data.csv")
summary = df.describe()
# Output results
print(summary.to_string())
with open("summary.json", "w") as f:
json.dump(summary.to_dict(), f)
print("Processing complete")
EOF""",
timeout=300
)
Advantages Over execute_code_sandbox
- More reliable for complex imports (no sandbox isolation issues)
- Full stdout/stderr access for debugging
- Longer timeout flexibility (60s default vs 30s)
- Direct filesystem access without ARTIFACT_PATH indirection
Disadvantages
- No persistent state between calls (each heredoc is a fresh process)
- Verbose for simple one-liners
- Shell escaping may be needed for complex string literals
Troubleshooting
| Issue | Solution |
|---|---|
EOF not recognized |
Ensure EOF is on its own line with no leading spaces |
| Import errors | Verify packages are installed in the shell environment |
| File not found | Use absolute paths or check working directory with pwd |
| Syntax errors | Check Python indentation within heredoc |
See Also
- Use
execute_code_sandboxfor simpler scripts when available - Use
shell_agentfor tasks requiring autonomous iteration and error recovery - Consider writing scripts to
.pyfiles for complex, reusable code
常见问题
结束标记为什么必须位于行首?
只有当结束标记没有前导空格时,shell 才能识别它。
为什么使用带引号的标记?
带引号的标记可以防止 shell 改变 Python 代码中的变量或符号。
脚本在多次运行之间会保留状态吗?
这项技能指出,这种方法适用于不需要在多次调用之间保留状态的任务。
详情
作者的原始描述
Execute complex Python code via run_shell heredoc when execute_code_sandbox fails
repo: HKUDS/AgentPilot
热度
- GitHub 星标
- 7.8k
- 排名
- #13002
- 许可证
- 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帮你的智能体规划小型实验、阅读实验结果并记录发现,同时研究如何改进一个程序。