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

python-shell-fallback

Skill này làm gì

Skill này giúp AI agent chuyển các công việc Python dựa trên thư viện sang shell khi sandbox thiếu gói. Nó dành cho những người cần PDF, biểu đồ hoặc đầu ra cần nhiều thư viện.

Khi nào nên dùng

  • ✓Khi sandbox trả về ModuleNotFoundError hoặc lỗi import tương tự.
  • ✓Khi bạn cần một PDF hoặc biểu đồ mà sandbox không có thư viện cần thiết.
  • ✓Khi các lần chạy sandbox liên tục thất bại vì vấn đề về gói.

Liên kết skill

https://github.com/HKUDS/AgentPilot/blob/main/openspace/benchmarks/gdpval/skills/python-shell-fallback/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-shell-fallback/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

“Tạo báo cáo PDF từ đoạn văn bản này. Sandbox báo thiếu một module, nên hãy chạy nó trong shell.”

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

Cách hoạt động

Agent nhận ra lỗi thiếu gói, rồi viết lại mã để chạy qua shell bằng heredoc. Môi trường shell thường có quyền truy cập nhiều gói đã cài hơ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 Shell Fallback

When execute_code_sandbox fails for Python code that depends on external libraries, use run_shell with a heredoc Python script as a reliable fallback. The shell environment typically has better access to installed packages and system configuration than the sandbox.

When to Use This Pattern

Apply this skill when:

  • execute_code_sandbox returns errors related to missing libraries (e.g., ModuleNotFoundError)
  • The code requires packages that may not be installed in the sandbox environment
  • You need PDF generation, data visualization, or other library-heavy operations
  • Previous sandbox execution attempts have failed with import errors

How to Implement

Step 1: Identify the Failure

Check if the sandbox error indicates a library/dependency issue:

  • ModuleNotFoundError: No module named 'xxx'
  • ImportError: cannot import name 'xxx'
  • Similar package-related errors

Step 2: Convert to Shell Execution

Rewrite the code execution using run_shell with a heredoc:

python3 << 'EOF'
# Your Python code here
import library_name
# ... rest of code
EOF

Step 3: Execute via run_shell

Call run_shell with the heredoc Python script:

command: python3 << 'EOF'
import reportlab
from reportlab.lib.pagesizes import letter
from reportlab.pdfgen import canvas

# Your library-dependent code here
c = canvas.Canvas("output.pdf", pagesize=letter)
c.drawString(100, 750, "Hello World")
c.save()
EOF

Example: PDF Generation Fallback

Sandbox attempt (fails):

# execute_code_sandbox call fails with:
# ModuleNotFoundError: No module named 'reportlab'

Shell fallback (works):

python3 << 'EOF'
from reportlab.lib.pagesizes import letter
from reportlab.pdfgen import canvas

c = canvas.Canvas("legal_memo.pdf", pagesize=letter)
c.setTitle("Legal Memorandum")
c.drawString(100, 750, "CONFIDENTIAL LEGAL MEMORANDUM")
c.save()
print("PDF created successfully")
EOF

Best Practices

  1. Use quoted heredoc (<< 'EOF') - Prevents variable expansion, keeping Python code intact
  2. Verify output - Check stdout/stderr from run_shell to confirm success
  3. Handle file paths - Ensure output files are written to accessible directories
  4. Test incrementally - For complex scripts, test in smaller chunks first
  5. Include error handling - Add try/except blocks to capture issues gracefully

Alternative: Inline Python Command

For simple one-liners or short scripts:

python3 -c "import reportlab; print(reportlab.__version__)"

Troubleshooting

If shell execution also fails:

  • Check if Python3 is available: which python3
  • Verify package installation: pip3 list | grep package_name
  • Try python instead of python3 (some systems differ)
  • Consider installing the package: pip3 install package_name
Câu hỏi thường gặp

Lỗi nào báo cho agent biết cần chuyển?

Các lỗi như ModuleNotFoundError hoặc ImportError về một gói là tín hiệu để chuyển.

Nó có thể tạo file PDF không?

Skill nhắc đến việc tạo PDF bằng một thư viện như reportlab làm ví dụ.

Điều này có luôn thành công không?

Skill giả định shell có các gói, nên vẫn có thể thất bại nếu chúng chưa được cài ở đó.

Chi tiết

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

Fallback to shell execution when sandbox fails for library-dependent Python code

repo: HKUDS/AgentPilot

Độ phổ biến

Sao GitHub
7.8k
Vị trí xếp hạng
#12961
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ự