python-shell-fallback
它能做什么
这项技能帮助 AI 智能体在沙箱缺少某个软件包时,将依赖库的 Python 工作转到 shell 中进行。它适合需要 PDF、图表或其他依赖库较多的输出的人。
适用场景
- ✓当沙箱返回 ModuleNotFoundError 或类似的导入错误的时候。
- ✓当你需要 PDF 或图表,而沙箱中没有所需库的时候。
- ✓当沙箱运行因软件包问题而持续失败的时候。
技能链接
https://github.com/HKUDS/AgentPilot/blob/main/openspace/benchmarks/gdpval/skills/python-shell-fallback/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“请把这段文字制作成 PDF 报告。沙箱提示缺少某个模块,请改在 shell 中运行。”
每周 5 个新技能,1 分钟读完。
工作原理
智能体会识别缺少软件包的错误,然后改写代码,借助 heredoc 通过 shell 运行。shell 环境通常可以访问更多已安装的软件包。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
Python Shell Fallback
When execute_code_sandbox fails for Python code that depends on external libraries, use run_shell with a heredoc Python script as a reliable fallback. The shell environment typically has better access to installed packages and system configuration than the sandbox.
When to Use This Pattern
Apply this skill when:
execute_code_sandboxreturns errors related to missing libraries (e.g.,ModuleNotFoundError)- The code requires packages that may not be installed in the sandbox environment
- You need PDF generation, data visualization, or other library-heavy operations
- Previous sandbox execution attempts have failed with import errors
How to Implement
Step 1: Identify the Failure
Check if the sandbox error indicates a library/dependency issue:
ModuleNotFoundError: No module named 'xxx'ImportError: cannot import name 'xxx'- Similar package-related errors
Step 2: Convert to Shell Execution
Rewrite the code execution using run_shell with a heredoc:
python3 << 'EOF'
# Your Python code here
import library_name
# ... rest of code
EOF
Step 3: Execute via run_shell
Call run_shell with the heredoc Python script:
command: python3 << 'EOF'
import reportlab
from reportlab.lib.pagesizes import letter
from reportlab.pdfgen import canvas
# Your library-dependent code here
c = canvas.Canvas("output.pdf", pagesize=letter)
c.drawString(100, 750, "Hello World")
c.save()
EOF
Example: PDF Generation Fallback
Sandbox attempt (fails):
# execute_code_sandbox call fails with:
# ModuleNotFoundError: No module named 'reportlab'
Shell fallback (works):
python3 << 'EOF'
from reportlab.lib.pagesizes import letter
from reportlab.pdfgen import canvas
c = canvas.Canvas("legal_memo.pdf", pagesize=letter)
c.setTitle("Legal Memorandum")
c.drawString(100, 750, "CONFIDENTIAL LEGAL MEMORANDUM")
c.save()
print("PDF created successfully")
EOF
Best Practices
- Use quoted heredoc (
<< 'EOF') - Prevents variable expansion, keeping Python code intact - Verify output - Check stdout/stderr from
run_shellto confirm success - Handle file paths - Ensure output files are written to accessible directories
- Test incrementally - For complex scripts, test in smaller chunks first
- Include error handling - Add try/except blocks to capture issues gracefully
Alternative: Inline Python Command
For simple one-liners or short scripts:
python3 -c "import reportlab; print(reportlab.__version__)"
Troubleshooting
If shell execution also fails:
- Check if Python3 is available:
which python3 - Verify package installation:
pip3 list | grep package_name - Try
pythoninstead ofpython3(some systems differ) - Consider installing the package:
pip3 install package_name
常见问题
哪些错误会让智能体切换?
ModuleNotFoundError 或与某个软件包相关的 ImportError 等错误,是切换的信号。
它能制作 PDF 文件吗?
这项技能提到了使用 reportlab 等库生成 PDF,作为其中一个例子。
这一定会成功吗?
这项技能假设 shell 中已安装所需的软件包,因此如果那里没有安装,仍然可能失败。
详情
作者的原始描述
Fallback to shell execution when sandbox fails for library-dependent Python code
repo: HKUDS/AgentPilot
热度
- GitHub 星标
- 7.8k
- 排名
- #12961
- 许可证
- 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帮你的智能体规划小型实验、阅读实验结果并记录发现,同时研究如何改进一个程序。