Execute complex Python code via run_shell heredoc when execute_code_sandbox fails
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.
execute_code_sandbox has failed 2+ times with unexplained errorsrun_shell(
command="""python3 << 'EOF'
import sys
# Your Python code here
print("Hello from heredoc")
EOF""",
timeout=300
)
<< 'EOF' (with single quotes) to prevent shell variable expansionEOF on its own line (no leading whitespace)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
)
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
)
| 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 |
execute_code_sandbox for simpler scripts when availableshell_agent for tasks requiring autonomous iteration and error recovery.py files for complex, reusable code37:["$","$L3e",null,{"content":"$3f","frontMatter":{"name":"shell-python-heredoc","description":"Execute complex Python code via run_shell heredoc when execute_code_sandbox fails"}}]