Fallback method for executing Python when execute_code_sandbox fails with unknown errors
复制下面这句话,粘贴给 Claude Code、Codex、Cursor 等 AI 编程工具,它会读取安装说明并在你确认后完成安装。
请阅读 https://ai.atlankj.com/install/asset/gh-python-execution-fallback-9d4989-680f94ab8c3f ,按照其中的说明把「python-execution-fallback-9d4989」安装到你(当前 AI 工具)中。执行前先告诉我将运行的命令和写入的位置,等我确认。
查看 AI 将读取的安装说明正在读取 GitHub 原文…
内容来自 GitHub 原始文件,由原作者维护。在 GitHub 查看
Use this skill when execute_code_sandbox returns 'unknown error' or fails unexpectedly, even on simple Python code like print statements.
Switch to using run_shell with Python heredoc syntax instead of execute_code_sandbox.
python3 << 'EOF'
# Your Python code here
print("Hello World")
result = 2 + 2
print(f"Result: {result}")
EOF
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")
run_shell executes commands directly in the shell environment, bypassing sandbox issues<< 'EOF') allows clean multi-line Python code without complex escaping'EOF') to prevent variable expansion.py file first, then executing:
run_shell(command="python3 /path/to/script.py")
# 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()
| 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 |