Fallback to shell execution when sandbox fails for library-dependent Python code
复制下面这句话,粘贴给 Claude Code、Codex、Cursor 等 AI 编程工具,它会读取安装说明并在你确认后完成安装。
请阅读 https://ai.atlankj.com/install/asset/gh-python-shell-fallback-024502ec9172 ,按照其中的说明把「python-shell-fallback」安装到你(当前 AI 工具)中。执行前先告诉我将运行的命令和写入的位置,等我确认。
查看 AI 将读取的安装说明正在读取 GitHub 原文…
内容来自 GitHub 原始文件,由原作者维护。在 GitHub 查看
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.
Apply this skill when:
execute_code_sandbox returns errors related to missing libraries (e.g., ModuleNotFoundError)Check if the sandbox error indicates a library/dependency issue:
ModuleNotFoundError: No module named 'xxx'ImportError: cannot import name 'xxx'Rewrite the code execution using run_shell with a heredoc:
python3 << 'EOF'
# Your Python code here
import library_name
# ... rest of code
EOF
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
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
<< 'EOF') - Prevents variable expansion, keeping Python code intactrun_shell to confirm successFor simple one-liners or short scripts:
python3 -c "import reportlab; print(reportlab.__version__)"
If shell execution also fails:
which python3pip3 list | grep package_namepython instead of python3 (some systems differ)pip3 install package_name