Skip to content
Skillpedia
General coding7.8k · #12960

python-heredoc-fallback

What it does

This skill shows the AI agent how to run Python in the shell with a heredoc when the sandbox is not working. It is for people who need files such as charts, Word documents or spreadsheets made by Python.

When to use it

  • ✓When the code sandbox fails with setup or connection errors.
  • ✓When you need a chart image, Word document or spreadsheet created by Python.
  • ✓When a file generation task keeps failing in the sandbox.

Skill link

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

Copy it and paste it into your AI (Claude, ChatGPT or Cursor), asking it to follow the instructions.

How to use

The easiest way: copy the message and paste it into your AI. It reads the skill and follows the instructions.

Ready-to-paste message

Use this skill to help me: https://github.com/HKUDS/AgentPilot/blob/main/openspace/benchmarks/gdpval/skills/python-heredoc-fallback/SKILL.md Read the SKILL.md file at that link and follow its instructions for my task.
Want your AI to find skills like this on its own?Install the free Skillpedia MCP: one line, and it searches 15,000+ skills before each task.Install in 1 minute →

Example of what to ask your AI

“Make a bar chart from my input.xlsx and save it as a PNG. The sandbox keeps failing.”

5 new skills a week. 1 minute to read.

How it works

The agent installs any needed packages first, then runs the Python code in the shell with a heredoc. The code creates the file and prints a message when it finishes.

Technical content (original SKILL.md)

Original text by the author, straight from GitHub.

Python Heredoc Fallback for Code Execution

When to Use

Use this pattern when execute_code_sandbox fails with e2b initialization errors, sandbox connection issues, or other execution environment problems. This is especially reliable for file generation tasks.

Core Pattern

Instead of execute_code_sandbox, run Python code directly in the shell using a heredoc:

python3 << 'EOF'
# Your Python code here
import sys
print("Python code executing in shell")
EOF

Key Syntax Rules

  1. Use single-quoted EOF (<< 'EOF') to prevent shell variable expansion within the Python code
  2. No indentation before EOF - the closing delimiter must be at the start of the line
  3. Install packages first if needed, before the Python heredoc block

Common Use Cases

1. Matplotlib Graph Generation

python3 << 'EOF'
import matplotlib.pyplot as plt
import pandas as pd

# Load data
data = pd.read_excel('input.xlsx')

# Create visualization
plt.figure(figsize=(10, 6))
plt.plot(data['x'], data['y'])
plt.xlabel('X Label')
plt.ylabel('Y Label')
plt.title('Graph Title')
plt.savefig('output_graph.png', dpi=300, bbox_inches='tight')
plt.close()

print("Graph saved successfully")
EOF

2. Python-docx Document Creation

pip install python-docx -q

python3 << 'EOF'
from docx import Document
from docx.shared import Inches, Pt
from docx.enum.text import WD_ALIGN_PARAGRAPH

doc = Document()

# Add title
title = doc.add_heading('Document Title', 0)
title.alignment = WD_ALIGN_PARAGRAPH.CENTER

# Add sections
doc.add_heading('Section 1', level=1)
doc.add_paragraph('Content for section 1.')

# Add table
table = doc.add_table(rows=3, cols=3)
table.style = 'Table Grid'

# Save document
doc.save('output.docx')
print("Document created successfully")
EOF

3. General File Operations

python3 << 'EOF'
import json
import os

# Read input file
with open('input.json', 'r') as f:
    data = json.load(f)

# Process data
result = {'processed': True, 'items': len(data)}

# Write output
with open('output.json', 'w') as f:
    json.dump(result, f, indent=2)

print(f"Processed {result['items']} items")
EOF

Best Practices

  1. Install dependencies first: Run pip install package-name -q before the Python block if packages aren't guaranteed to be available
  2. Verify file paths: Files are read from and written to the current working directory
  3. Add print statements: Include progress/output messages to verify execution succeeded
  4. Close resources: Explicitly close matplotlib figures and file handles
  5. Handle errors gracefully: Add try/except blocks for production code

Troubleshooting

Issue Solution
Module not found Add pip install <module> -q before Python block
File not found Verify file exists in current directory with ls
Permission denied Check file permissions and directory write access
Syntax errors Ensure proper Python indentation within heredoc

Advantages Over execute_code_sandbox

  • No initialization overhead - runs immediately in existing shell
  • Persistent environment - maintains state across multiple commands
  • Direct file system access - no sandbox isolation issues
  • Full system Python - access to all installed packages
  • Reliable for large files - no sandbox size restrictions
FAQ

What kinds of files can it make?

The skill gives examples for charts, Word documents and spreadsheets made with Python libraries.

Does the agent install packages?

Yes, the skill says to install any needed packages before the Python block runs.

Why must the closing EOF line start at the left edge?

The shell only reads the end marker when it sits at the start of a line.

Details

Original description by the author

Use shell Python heredoc as fallback when code sandbox fails

repo: HKUDS/AgentPilot

Popularity

GitHub stars
7.8k
Ranking position
#12960
License
MIT
Updated
11/1/2026

Is this skill useful?

0

Sign in to vote.

Comments

  • …

More skills from this repository

Similar skills