python-execution-fallback-9d4989
Fallback method for executing Python when execute_code_sandbox fails with unknown errors
- License 许可
- MIT
- 最近更新
- 2026-04-16
- Stars
- ★ 0
- Forks
- 0
01 前言 Front Matter
- name
- python-execution-fallback-9d4989
- description
- Fallback method for executing Python when execute_code_sandbox fails with unknown errors
02 资源文件 1
- 其他.skill_id46 B
03 SKILL.md 正文
Python Execution Fallback
When to Use
Use this skill when execute_code_sandbox returns 'unknown error' or fails unexpectedly, even on simple Python code like print statements.
Solution
Switch to using run_shell with Python heredoc syntax instead of execute_code_sandbox.
Basic Syntax
python3 << 'EOF'
# Your Python code here
print("Hello World")
result = 2 + 2
print(f"Result: {result}")
EOF
How to Call
Instead of:
execute_code_sandbox(code="print('Hello')")
Use:
run_shell(command="python3 << 'EOF'\nprint('Hello')\nEOF")
For multi-line code, build the command string:
code = """
import json
data = {"status": "ok", "count": 42}
print(json.dumps(data))
"""
run_shell(command=f"python3 << 'EOF'\n{code}\nEOF")
Why This Works
run_shellexecutes commands directly in the shell environment, bypassing sandbox issues- Heredoc syntax (
<< 'EOF') allows clean multi-line Python code without complex escaping - Single quotes around EOF prevent unwanted shell variable expansion
- More reliable for simple to moderate Python execution tasks
Guidelines
- Always use single quotes around EOF (
'EOF') to prevent variable expansion - Test with a simple print statement first to verify Python is available
- For longer scripts, consider writing to a
.pyfile first, then executing:run_shell(command="python3 /path/to/script.py") - Capture stdout from run_shell to get Python output
- Handle errors by checking run_shell's stderr output
Example: Complete Workflow
# Step 1: Try execute_code_sandbox
try:
result = execute_code_sandbox(code="print(2+2)")
except Exception as e:
# Step 2: Fallback to run_shell with heredoc
result = run_shell(command="python3 << 'EOF'\nprint(2+2)\nEOF")
# Parse stdout to get the output
output = result.stdout.strip()
Troubleshooting
| Issue | Solution |
|---|---|
| Python not found | Use which python3 to verify installation |
| Heredoc not working | Ensure single quotes around EOF delimiter |
| Complex imports fail | Install required packages or write to .py file |
| Output not captured | Check run_shell stdout, not return value |
04 同仓库更多 Skills
write-special-chars-ts-check
Workaround for write_file failures caused by special characters (apostrophes, backticks, template literals) using a Python heredoc script, plus a reliable TypeScript type-checking step that filters output to only the newly-written files to avoid being misled by pre-existing errors.
write-special-chars-readback
Workaround for write_file failures caused by special characters (apostrophes, backticks, template literals) using a Python heredoc script, with mandatory post-write verification via read_file and cleanup of /tmp helper scripts.
write-special-chars-heredoc
Workaround for write_file failures caused by special characters (apostrophes, backticks, template literals) by using a Python heredoc script executed via shell.
worldmonitor-reference
Curated architecture index of the WorldMonitor open-source dashboard. Reference for extracting patterns — vanilla TypeScript, Panel class hierarchy, service layer, CSS grid, API edge functions, refresh scheduler, and 60+ data source integrations.
virtual-list
Implement efficient virtual scrolling for rendering large lists with DOM recycling, chunk-based rendering, and performance optimizations
verify-directory-structure
Diagnose and resolve file access issues by first verifying the directory structure.