pdf-debug-unbuffered
它能做什么
这项技能是调试 Python PDF 脚本的一个技巧,适用于脚本只给出模糊错误的情况。适合 PDF 代码停止运行、却没有给出有用原因的人。
适用场景
- ✓当 Python PDF 脚本只显示模糊的未知错误时。
- ✓当脚本似乎无声失败、没有出现回溯信息时。
- ✓当我怀疑 PDF 代码中缺少字体、导入或文件路径时。
技能链接
https://github.com/HKUDS/AgentPilot/blob/main/openspace/benchmarks/gdpval/skills/pdf-debug-unbuffered/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“请帮我:我的 Python PDF 脚本报错,提示内容没有任何用处。请运行一下,让我们看到真正的错误。”
每周 5 个新技能,1 分钟读完。
工作原理
AI 使用无缓冲参数运行脚本,把错误输出与普通输出合并,然后阅读最前面几行找出回溯信息。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
PDF Generation Debug Technique
When Python PDF libraries (reportlab, fpdf, etc.) fail with generic "unknown error" messages, the actual cause is often hidden due to output buffering or error swallowing. This skill provides a technique to expose the real traceback.
Core Technique
Run the Python script with the -u flag (unbuffered output) and pipe stderr through head to capture the actual error:
python -u your_script.py 2>&1 | head -100
Or more specifically, to focus on stderr:
python -u your_script.py 2>&1 | head -50
Why This Works
-uflag: Forces Python to run in unbuffered mode, ensuring output (including errors) is flushed immediately rather than held in buffers that may be lost on crash2>&1: Redirects stderr to stdout so both streams are captured togetherhead: Limits output to show the most relevant error messages at the top
Step-by-Step Debugging
Step 1: Identify the failing script
Locate the Python script that generates the PDF and is producing generic errors.
Step 2: Run with unbuffered output
python -u generate_pdf.py 2>&1 | head -100
Step 3: Analyze the traceback
Look for:
- Import errors (missing dependencies)
- File path issues (invalid paths, permission errors)
- Font registration problems (common with reportlab)
- Memory or resource constraints
Step 4: Fix the underlying issue
Address the specific error revealed in the traceback.
Step 5: Re-run normally
Once fixed, run the script without the debug flags to confirm it works:
python generate_pdf.py
Common Issues Revealed
| Symptom | Likely Cause |
|---|---|
| "unknown error" on PDF save | File path doesn't exist or no write permissions |
| Generic failure during build | Missing font files or font registration issues |
| Silent crash | ImportError for missing dependencies |
| Incomplete PDF | Script terminated early due to unhandled exception |
Alternative Approaches
If the above doesn't work, try:
# Capture full stderr to a file
python -u script.py 2> error.log
# Use Python's traceback module explicitly
python -c "import traceback; exec(open('script.py').read())" 2>&1
# Run with verbose import tracing
python -v -u script.py 2>&1 | head -200
Example: Debugging reportlab
# Before: Generic error
python create_checklist.py
# Output: "Error: unknown"
# After: Actual traceback
python -u create_checklist.py 2>&1 | head -50
# Output: "FileNotFoundError: [Errno 2] No such file or directory: '/fonts/Helvetica.ttf'"
Notes
- This technique applies to any Python script with opaque error messages, not just PDF generation
- The
-uflag is available in Python 2.7+ and all Python 3 versions - For production debugging, consider logging to a file instead of using
head
常见问题
无缓冲参数有什么作用?
它让 Python 立即输出信息,即使程序崩溃,消息也不会丢失。
为什么要用 head 限制输出?
限制输出可以让最重要的错误信息保留在最前面,便于阅读。
它能发现哪些类型的错误?
指南提到了缺少导入、文件路径问题、字体注册问题和资源限制。
详情
作者的原始描述
Debug Python PDF generation errors by using unbuffered output and stderr inspection to reveal actual tracebacks
repo: HKUDS/AgentPilot
热度
- GitHub 星标
- 7.8k
- 排名
- #12914
- 许可证
- 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帮你的智能体规划小型实验、阅读实验结果并记录发现,同时研究如何改进一个程序。