跳到正文
Skillpedia
通用编程7.8k · #12962

python-shell-workaround

它能做什么

这项技能为使用 openpyxl、python-docx 或 pandas 等软件包的 Python 代码提供了变通方法。它适合沙箱中缺少这些软件包的人。

适用场景

  • ✓当沙箱在导入 openpyxl 或 pandas 等软件包的代码上失败的时候。
  • ✓当你需要用一行 Python 命令快速测试某个软件包的时候。
  • ✓当较长的 Python 任务应在 shell 中而不是沙箱中运行的时候。

技能链接

https://github.com/HKUDS/AgentPilot/blob/main/openspace/benchmarks/gdpval/skills/python-shell-workaround/SKILL.md

复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。

使用方法

最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。

可直接粘贴的消息

请用这个技能帮我:https://github.com/HKUDS/AgentPilot/blob/main/openspace/benchmarks/gdpval/skills/python-shell-workaround/SKILL.md 阅读该链接中的 SKILL.md 文件,并在我的任务中按照它的说明来做。
想让你的 AI 自己找到这样的技能吗?安装免费的 Skillpedia MCP:只需一行,每次任务前它都会在 15,000+ 个技能中搜索。1 分钟完成安装 →

可以这样问 AI

“请用 openpyxl 创建一个带表头行的 Excel 文件。沙箱失败了,请换一种方式运行。”

每周 5 个新技能,1 分钟读完。

工作原理

智能体会把 Python 代码改写为一行 shell 命令或 heredoc,并通过 shell 工具运行。然后它检查输出,确认文件已经生成。

技术内容(原始 SKILL.md)

作者在 GitHub 上的英文原文。

Python Shell Workaround

When to Use This Skill

Use this pattern when execute_code_sandbox fails with Python code that requires external packages (like openpyxl, python-docx, pandas, etc.) that may not be available in the sandbox environment.

The Pattern

Instead of relying solely on execute_code_sandbox, use run_shell with inline Python commands as a fallback:

python3 -c "your python code here"

Step-by-Step Instructions

Step 1: Detect Sandbox Failure

Watch for errors like:

  • ModuleNotFoundError for common packages
  • Import errors for external libraries
  • Sandbox environment limitations

Step 2: Convert Python Code to Shell Command

Transform your Python script into a one-liner or use a heredoc for longer scripts:

For simple code:

python3 -c "import openpyxl; wb = openpyxl.Workbook(); wb.save('file.xlsx')"

For complex code (using heredoc):

python3 << 'EOF'
import openpyxl
from openpyxl.utils import get_column_letter

wb = openpyxl.Workbook()
ws = wb.active
ws['A1'] = 'Header'
wb.save('output.xlsx')
print('File created successfully')
EOF

Step 3: Execute with run_shell

Use run_shell to execute the command:

tool: run_shell
command: python3 -c "import openpyxl; print('Package available')"

Step 4: Verify Output

Check the stdout/stderr from run_shell to confirm success, then proceed with subsequent steps.

Best Practices

  1. Escape quotes properly: Use single quotes inside double-quoted strings or vice versa
  2. Use heredocs for multiline code: Cleaner and easier to debug
  3. Install packages if needed: Add pip install package before your Python command if packages are missing
  4. Chain commands safely: Use && to ensure prerequisites succeed before running Python code

Example Scenarios

Creating Excel Files

python3 << 'EOF'
import openpyxl
from openpyxl.worksheet.datavalidation import DataValidation

wb = openpyxl.Workbook()
ws = wb.active
ws.title = "Data"

# Add headers
headers = ['Name', 'Email', 'Status']
for col, header in enumerate(headers, 1):
    ws.cell(row=1, column=col, value=header)

wb.save('output.xlsx')
EOF

Creating Word Documents

python3 -c "
from docx import Document
doc = Document()
doc.add_heading('Report', 0)
doc.save('report.docx')
print('Document created')
"

Limitations

  • Complex multiline scripts are harder to maintain as one-liners
  • Debugging is less convenient than in sandbox
  • Some packages may still not be available in the shell environment

Related Patterns

  • If packages are truly unavailable, consider installing them first: pip install package_name && python3 -c "..."
  • For persistent scripts, write to a .py file first, then execute with python3 script.py
常见问题

什么时候用单行命令,而不用 heredoc?

单行命令适合简短的代码,而 heredoc 适合包含多个步骤的较长脚本。

智能体如何知道运行成功了?

它会检查打印的输出,然后确认文件存在。

这项技能提到了哪些软件包?

它提到 openpyxl、python-docx 和 pandas,作为沙箱可能缺少的软件包的例子。

详情

作者的原始描述

Workaround for executing Python code with external packages when sandbox fails

repo: HKUDS/AgentPilot

热度

GitHub 星标
7.8k
排名
#12962
许可证
MIT
更新时间
2026/11/1

这个技能有用吗?

0

登录 后即可投票。

评论

  • …

同一仓库的更多技能

相似技能