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

reliable-script-execution

它能做什么

这项技能告诉 AI 智能体,在运行 Python 代码之前先把它保存为文件。它适合内联运行的 Python 总是因未知错误而失败的人。

适用场景

  • ✓当内联 Python 代码因未知错误失败,尤其是多行脚本的时候。
  • ✓当代码中有许多引号或特殊字符,导致内联运行出错的时候。
  • ✓当你希望保留所运行脚本的记录的时候。

技能链接

https://github.com/HKUDS/AgentPilot/blob/main/openspace/benchmarks/gdpval/skills/reliable-script-execution/SKILL.md

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

使用方法

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

可直接粘贴的消息

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

可以这样问 AI

“请编写一个简短的 Python 脚本,生成我的开支 JSON 报告,保存后运行它。”

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

工作原理

智能体会把代码写入一个清晰文件夹中的 .py 文件,然后通过 shell 在设定的时间限制内运行它。任务完成后,它可以删除这个临时文件。

技术内容(原始 SKILL.md)

作者在 GitHub 上的英文原文。

Reliable Script Execution

When executing Python code via shell commands, avoid inline heredoc execution which can fail unpredictably with 'unknown error'. Use this two-step file-first approach for more reliable script execution.

Problem

Direct heredoc Python execution like:

python3 << 'EOF'
# complex code here
EOF

Can fail with 'unknown error', especially when:

  • Script contains multiple lines or complex logic
  • Special characters or quotes are present
  • Working directory context matters

Solution: File-First Approach

Step 1: Write Python Script to File

Use write_file to save your Python code to a .py file:

write_file(path="./temp_script.py", content="""
import json

data = {"key": "value"}
print(json.dumps(data))
""")

Step 2: Execute via Shell

Use run_shell with explicit working directory:

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

Step 3: Clean Up (Optional)

Remove temporary files after execution:

run_shell(command="rm ./temp_script.py")

Complete Example

Task: Generate a JSON report with calculations

# Step 1: Write the script
write_file(
    path="./generate_report.py",
    content="""
import json
from datetime import datetime

revenue = 500000.00
expenses = 379577.06
net_income = revenue - expenses

report = {
    "generated": datetime.now().isoformat(),
    "revenue": revenue,
    "expenses": expenses,
    "net_income": net_income
}

print(json.dumps(report, indent=2))
"""
)

# Step 2: Execute
run_shell(command="python3 ./generate_report.py", timeout=60)

# Step 3: Clean up
run_shell(command="rm ./generate_report.py")

Best Practices

  1. Use descriptive filenames: Name scripts according to their purpose (e.g., calculate_pnl.py, transform_data.py)

  2. Set appropriate timeouts: For data processing scripts, use longer timeouts (60-300 seconds)

  3. Specify working directory: If the script depends on relative paths, include cd /path && python3 script.py

  4. Handle errors gracefully: Check shell output for errors and retry if needed

  5. Clean up temporary files: Don't leave .py files cluttering the workspace unless they need to persist

When to Use This Pattern

  • Executing multi-line Python code via shell
  • Scripts with complex string handling or special characters
  • When heredoc execution has failed previously
  • Any scenario where reliability matters more than brevity

Anti-Pattern to Avoid

Do NOT rely on heredoc for production-critical scripts:

# Unreliable - may fail with 'unknown error'
python3 << 'EOF'
# Your code here
EOF

Use file-first approach instead for consistent, debuggable execution.

常见问题

为什么文件比内联代码更可靠?

这项技能指出,内联 heredoc 运行可能出现不可预测的失败,而保存的文件则给出更清晰的结果。

之后应该删除文件吗?

这项技能说明清理是可选的,因此如果你想保留记录,也可以保留文件。

我可以设置时间限制吗?

可以。这项技能的示例会向 shell 命令传入超时参数。

详情

作者的原始描述

Execute Python scripts reliably using file-first approach instead of heredoc

repo: HKUDS/AgentPilot

热度

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

这个技能有用吗?

0

登录 后即可投票。

评论

  • …

同一仓库的更多技能

相似技能