跳到正文
Skillpedia
通用编程7.8k · #12961

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。它会读取这个技能并按说明操作。

可直接粘贴的消息

请用这个技能帮我:https://github.com/HKUDS/AgentPilot/blob/main/openspace/benchmarks/gdpval/skills/python-shell-fallback/SKILL.md 阅读该链接中的 SKILL.md 文件,并在我的任务中按照它的说明来做。
想让你的 AI 自己找到这样的技能吗?安装免费的 Skillpedia MCP:只需一行,每次任务前它都会在 15,000+ 个技能中搜索。1 分钟完成安装 →

可以这样问 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_sandbox returns 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

  1. Use quoted heredoc (<< 'EOF') - Prevents variable expansion, keeping Python code intact
  2. Verify output - Check stdout/stderr from run_shell to confirm success
  3. Handle file paths - Ensure output files are written to accessible directories
  4. Test incrementally - For complex scripts, test in smaller chunks first
  5. 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 python instead of python3 (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

这个技能有用吗?

0

登录 后即可投票。

评论

  • …

同一仓库的更多技能

相似技能