Chuyển đến nội dung
Skillpedia
Lập trình chung7.8k · #12958

python-execution-fallback-9d4989

Skill này làm gì

Skill này cho AI agent một cách dự phòng để chạy mã Python đơn giản khi công cụ sandbox bị hỏng. Nó dành cho những người có công cụ AI báo lỗi không rõ nguyên nhân ngay cả với đoạn mã Python ngắn.

Khi nào nên dùng

  • ✓Khi sandbox mã trả về lỗi không rõ nguyên nhân, ngay cả với một câu lệnh print đơn giản.
  • ✓Khi bạn cần một phép tính Python nhanh và sandbox không phản hồi.
  • ✓Khi bạn muốn kiểm tra xem Python có chạy được trên máy của AI hay không.

Liên kết skill

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

Sao chép rồi dán vào AI của bạn (Claude, ChatGPT hoặc Cursor) và nhờ nó làm theo hướng dẫn.

Cách dùng

Cách dễ nhất: sao chép tin nhắn và dán vào AI của bạn. AI sẽ đọc skill và làm theo hướng dẫn.

Tin nhắn có sẵn để dán

Hãy dùng skill này để giúp tôi: https://github.com/HKUDS/AgentPilot/blob/main/openspace/benchmarks/gdpval/skills/python-execution-fallback-9d4989/SKILL.md Đọc tệp SKILL.md tại liên kết đó và làm theo hướng dẫn cho việc của tôi.
Bạn muốn AI tự tìm những skill như thế này?Cài MCP Skillpedia miễn phí: chỉ một dòng, và AI sẽ tìm trong 15.000+ skill trước mỗi việc.Cài đặt trong 1 phút →

Ví dụ cách nhờ AI

“Sandbox mã liên tục lỗi. Hãy chạy phép tính Python này bằng cách khác và cho tôi xem kết quả.”

5 skill mới mỗi tuần. Đọc chỉ 1 phút.

Cách hoạt động

Agent gửi mã Python qua shell bằng heredoc, cách này đưa thẳng mã vào Python. Nó bắt đầu bằng một lệnh print thử nghiệm ngắn để xác nhận Python có sẵn.

Nội dung kỹ thuật (SKILL.md gốc)

Lấy nguyên văn từ GitHub, do tác giả viết bằng tiếng Anh.

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_shell executes 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

  1. Always use single quotes around EOF ('EOF') to prevent variable expansion
  2. Test with a simple print statement first to verify Python is available
  3. For longer scripts, consider writing to a .py file first, then executing:
    run_shell(command="python3 /path/to/script.py")
    
  4. Capture stdout from run_shell to get Python output
  5. 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
Câu hỏi thường gặp

Heredoc là gì?

Đó là cách truyền nhiều dòng mã cho một chương trình trong một lệnh mà không cần lưu trước.

Tôi có nên dùng cách này cho script dài không?

Skill khuyên nên lưu script dài thành file trước rồi chạy file đó.

Tại sao dùng dấu nháy đơn quanh EOF?

Dấu nháy đơn ngăn shell thay đổi dấu đô la hoặc các ký hiệu khác trong mã.

Chi tiết

Mô tả gốc của tác giả

Fallback method for executing Python when execute_code_sandbox fails with unknown errors

repo: HKUDS/AgentPilot

Độ phổ biến

Sao GitHub
7.8k
Vị trí xếp hạng
#12958
Giấy phép
MIT
Cập nhật
1/11/2026

Skill này có hữu ích không?

0

Đăng nhập để bình chọn.

Bình luận

  • …

Thêm skill từ kho mã này

Skill tương tự