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。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 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
- Escape quotes properly: Use single quotes inside double-quoted strings or vice versa
- Use heredocs for multiline code: Cleaner and easier to debug
- Install packages if needed: Add
pip install packagebefore your Python command if packages are missing - 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
.pyfile first, then execute withpython3 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
评论
- …
同一仓库的更多技能
agentpilot
7.8k帮你的智能体准备任务并运行 AgentPilot 循环,逐步改进项目:循环会提出修改、测试修改并为其打分。
openspace
7.8k帮你的智能体设置 OpenSpace,这是一个用于存储和获取智能体技能的工具,并通过 MCP 将其连接到你的 AI 应用。
rsi-arena
7.8k帮你的智能体设置并运行标准 AI 基准测试,例如 AIME 和 Terminal-Bench,用来比较不同的改进方法。
experiment-guide
7.8k帮你的智能体规划小型实验、阅读实验结果并记录发现,同时研究如何改进一个程序。