Fallback workflow for executing Python code when execute_code_sandbox fails repeatedly
复制下面这句话,粘贴给 Claude Code、Codex、Cursor 等 AI 编程工具,它会读取安装说明并在你确认后完成安装。
请阅读 https://ai.atlankj.com/install/asset/gh-code-execution-fallback-e81068-c0014e0fb9b5 ,按照其中的说明把「code-execution-fallback-e81068」安装到你(当前 AI 工具)中。执行前先告诉我将运行的命令和写入的位置,等我确认。
查看 AI 将读取的安装说明正在读取 GitHub 原文…
内容来自 GitHub 原始文件,由原作者维护。在 GitHub 查看
Use this skill when execute_code_sandbox fails repeatedly (2+ attempts) with unknown, persistent, or unexplained errors. This fallback approach uses write_file + run_shell to save Python scripts to disk and execute them via command line, which has proven more reliable in certain failure scenarios.
Monitor execute_code_sandbox attempts. After 2 consecutive failures with errors like:
Switch to the fallback workflow immediately.
Use write_file to save your Python code as a .py file in the working directory:
write_file(
path="script.py",
content="""
import sys
import json
# Your Python code here
def main():
# Your logic
result = {"status": "success", "data": "example"}
print(json.dumps(result))
if __name__ == "__main__":
main()
"""
)
Tips:
Use run_shell to execute the Python script via command line:
run_shell(
command="python3 script.py",
timeout=60 # Adjust timeout as needed
)
Alternative commands:
python script.py - if python3 alias isn't availablepython3 -u script.py - for unbuffered outputpython3 script.py arg1 arg2 - with argumentsCheck the stdout/stderr from run_shell to:
If the script writes output files, use read_file to retrieve results.
Remove temporary script files if they won't be reused:
run_shell(command="rm script.py")
Scenario: execute_code_sandbox failed twice while trying to process data.
Fallback execution:
# Step 1: Write the processing script
write_file(
path="process_data.py",
content="""
import pandas as pd
import json
def process():
data = [1, 2, 3, 4, 5]
result = {"sum": sum(data), "count": len(data)}
print(json.dumps(result))
# Also save to file for reliability
with open("result.json", "w") as f:
json.dump(result, f)
if __name__ == "__main__":
process()
"""
)
# Step 2: Execute via shell
output = run_shell(command="python3 process_data.py")
# Step 3: Read results from file
results = read_file(file_path="result.json", filetype="json")
| Issue | Solution |
|---|---|
python3: command not found | Try python instead, or check available interpreters with which python |
| Permission denied | Ensure the working directory is writable; write_file creates files in workspace by default |
| Module not found | Install dependencies via run_shell(command="pip install package_name") before execution |
| Script hangs | Increase timeout parameter in run_shell |
| Output too long | Redirect output to file within the script and read it separately |
task_specific_script.py)print(f"Step X complete: {value}")execute_code_sandboxexecute_code_sandbox succeeds consistently (no need to add complexity)