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

code-exec-fallback

它能做什么

这项技能是在内置运行器反复失败时,运行 Python 代码的备用方法。它适合需要让 AI 的数据或计算脚本无论如何都能运行的人。

适用场景

  • ✓当运行器因为限制、超时或缺少软件包而失败两次或以上的时候。
  • ✓当我需要在设定的时间限制内,从 shell 运行数据脚本的时候。
  • ✓当我希望 AI 读取运行输出并报告任何错误的时候。

技能链接

https://github.com/HKUDS/AgentPilot/blob/main/openspace/benchmarks/gdpval/skills/code-exec-fallback/SKILL.md

复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。

使用方法

最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。

可直接粘贴的消息

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

可以这样问 AI

“Python 运行器在我的电子表格上一直超时。你能把脚本保存为文件,改为运行它吗?”

每周 5 个新技能,1 分钟读完。

工作原理

AI 会把脚本写入文件,在 shell 中用 python3 运行,在设定的时间内等待,并读取输出。之后可以删除该文件。

技术内容(原始 SKILL.md)

作者在 GitHub 上的英文原文。

Code Execution Fallback

When to Use

Use this pattern when execute_code_sandbox fails repeatedly (typically 2+ attempts) due to environment limitations, timeouts, dependency issues, or sandbox restrictions.

The Pattern

Instead of executing code directly in the sandbox, write the Python script to a file and execute it via shell:

  1. Write the script using write_file
  2. Execute via shell using run_shell with python3 script.py
  3. Clean up (optional) remove the temporary file

Step-by-Step Instructions

Step 1: Write the Python Script

Use write_file to save your Python code:
- Path: Choose a descriptive name (e.g., "process_data.py", "analyze.py")
- Content: Your complete Python script with all imports and logic

Step 2: Execute via Shell

Use run_shell to execute:
- Command: "python3 <script_name>.py"
- Timeout: Set appropriately for your task (default 30s, increase if needed)

Step 3: Handle Output

- Capture stdout/stderr from run_shell
- Parse results as needed
- Optionally delete the script file after execution

Example

# Instead of this (which may fail):
execute_code_sandbox(code="import pandas as pd; df = pd.read_csv('data.csv')...")

# Do this:
write_file(path="analyze.py", content="""
import pandas as pd
import json

df = pd.read_csv('data.csv')
result = df.groupby('category').sum()
print(json.dumps(result.to_dict()))
""")

run_shell(command="python3 analyze.py", timeout=60)

Tips for Success

  1. Include all imports in the script file - the shell environment may differ from the sandbox
  2. Use absolute paths or ensure working directory is correct
  3. Add error handling to your script for better debugging
  4. Increase timeout for long-running operations (default is 30s)
  5. Print structured output (JSON) if you need to parse results
  6. Clean up temporary files after successful execution to avoid clutter

When This Helps

  • Sandbox has missing dependencies
  • Code execution times out in sandbox but would work in shell
  • File I/O operations are restricted in sandbox
  • Need to run external commands or system utilities
  • Complex multi-file projects that need proper file structure
常见问题

为什么用文件,而不用运行器?

指南说,在某些失败情况下,这种方式已被证明更可靠。

默认时间限制是多少?

默认为 30 秒,任务需要更长时间时可以调高。

脚本需要特殊设置吗?

脚本应包含所有导入和逻辑,使其能够独立运行。

详情

作者的原始描述

Fallback pattern for executing Python code when execute_code_sandbox fails

repo: HKUDS/AgentPilot

热度

GitHub 星标
7.8k
排名
#12845
许可证
MIT
更新时间
2026/11/1

这个技能有用吗?

0

登录 后即可投票。

评论

  • …

同一仓库的更多技能

相似技能