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

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

可直接粘贴的消息

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

可以这样问 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_shell executes 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

  1. Always use single quotes around EOF ('EOF') to prevent variable expansion
  2. Test with a simple print statement first to verify Python is available
  3. For longer scripts, consider writing to a .py file first, then executing:
    run_shell(command="python3 /path/to/script.py")
    
  4. Capture stdout from run_shell to get Python output
  5. 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

这个技能有用吗?

0

登录 后即可投票。

评论

  • …

同一仓库的更多技能

相似技能