本文へスキップ
Skillpedia
一般的なプログラミング7.8k · #13002

shell-python-heredoc

できること

このスキルは、サンドボックスが繰り返し失敗するとき、AIエージェントが複数行のPythonスクリプトをシェルで実行する方法を示します。複数のimportや分かりやすいエラー出力が必要なスクリプトの人向けです。

使う場面

  • ✓コードサンドボックスが、説明のないエラーで2回以上失敗したとき。
  • ✓reportlabやpandasなど、複数のimportを持つスクリプトが、1行に収めるには長すぎるとき。
  • ✓何が失敗したかを確かめるために、stdoutとstderrを表示する必要があるとき。

スキルのリンク

https://github.com/HKUDS/AgentPilot/blob/main/openspace/benchmarks/gdpval/skills/shell-python-heredoc/SKILL.md

コピーしてAI(Claude、ChatGPT、Cursor)に貼り付け、指示に従うよう頼んでください。

使い方

いちばん簡単な方法:メッセージをコピーしてAIに貼り付けるだけ。AIがスキルを読んで指示に従います。

貼り付け用メッセージ

このスキルを使って手伝ってください:https://github.com/HKUDS/AgentPilot/blob/main/openspace/benchmarks/gdpval/skills/shell-python-heredoc/SKILL.md このリンクのSKILL.mdファイルを読んで、私の作業でその指示に従ってください。
こういうスキルを、AIが自分で見つけてくれたら?無料のSkillpedia MCPをインストール。1行で、作業の前に15,000+のスキルから探してくれます。1分でインストール →

AIへの頼み方の例

“サンドボックスが2回失敗したので、複数行のReportLabスクリプトをシェルで実行してください。出力を見せてください。”

毎週5つの新しいスキル。1分で読めます。

仕組み

エージェントは、スクリプトを引用符付きの終了マーカーを持つヒアドキュメントで囲み、時間制限付きでpython3を実行します。次に、stdoutとstderrを読んで、作業がうまくいったかを確認します。

技術的な内容(元のSKILL.md)

作者による元のテキスト(英語)をGitHubからそのまま表示しています。

Shell Python Heredoc (Fallback Pattern)

Overview

When execute_code_sandbox fails repeatedly (e.g., with "unknown error"), use run_shell with a Python heredoc as a reliable fallback for executing multi-line Python scripts with imports.

When to Use

  • execute_code_sandbox has failed 2+ times with unexplained errors
  • You need to run Python code with multiple imports (e.g., ReportLab, pandas)
  • The script is too complex for a one-liner but doesn't require persistence between calls
  • You need better error visibility from raw stdout/stderr

How to Use

Basic Pattern

run_shell(
    command="""python3 << 'EOF'
import sys
# Your Python code here
print("Hello from heredoc")
EOF""",
    timeout=300
)

Key Syntax Rules

  1. Use << 'EOF' (with single quotes) to prevent shell variable expansion
  2. Indent Python code relative to the heredoc delimiter
  3. End with EOF on its own line (no leading whitespace)
  4. Capture output from stdout/stderr for debugging

Example: Generating a PDF with ReportLab

run_shell(
    command="""python3 << 'EOF'
from reportlab.lib.pagesizes import letter
from reportlab.pdfgen import canvas

c = canvas.Canvas("output.pdf", pagesize=letter)
c.drawString(100, 750, "Hello PDF")
c.save()
print("PDF created successfully")
EOF""",
    timeout=300
)

Example: Multi-file Data Processing

run_shell(
    command="""python3 << 'EOF'
import pandas as pd
import json

# Load and process data
df = pd.read_csv("data.csv")
summary = df.describe()

# Output results
print(summary.to_string())
with open("summary.json", "w") as f:
    json.dump(summary.to_dict(), f)
print("Processing complete")
EOF""",
    timeout=300
)

Advantages Over execute_code_sandbox

  • More reliable for complex imports (no sandbox isolation issues)
  • Full stdout/stderr access for debugging
  • Longer timeout flexibility (60s default vs 30s)
  • Direct filesystem access without ARTIFACT_PATH indirection

Disadvantages

  • No persistent state between calls (each heredoc is a fresh process)
  • Verbose for simple one-liners
  • Shell escaping may be needed for complex string literals

Troubleshooting

Issue Solution
EOF not recognized Ensure EOF is on its own line with no leading spaces
Import errors Verify packages are installed in the shell environment
File not found Use absolute paths or check working directory with pwd
Syntax errors Check Python indentation within heredoc

See Also

  • Use execute_code_sandbox for simpler scripts when available
  • Use shell_agent for tasks requiring autonomous iteration and error recovery
  • Consider writing scripts to .py files for complex, reusable code
よくある質問

終了マーカーはなぜ行の先頭に置く必要があるのですか?

シェルは、先頭に空白がないときだけ終了マーカーを認識するためです。

なぜ引用符付きのマーカーを使うのですか?

引用符付きのマーカーは、Pythonコード内の変数や記号をシェルが置き換えるのを防ぐためです。

スクリプトは実行間で状態を保持しますか?

スキルは、この方法は呼び出し間で状態を保持する必要がない作業向けだと述べています。

詳細

作者による元の説明

Execute complex Python code via run_shell heredoc when execute_code_sandbox fails

repo: HKUDS/AgentPilot

人気

GitHubのスター
7.8k
ランキング
#13002
ライセンス
MIT
更新日
2026/11/1

このスキルは役に立ちますか?

0

ログイン して投票しましょう。

コメント

  • …

このリポジトリの他のスキル

似ているスキル