跳到正文
Skillpedia
PDF、Word 与幻灯片7.8k · #12914

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。它会读取这个技能并按说明操作。

可直接粘贴的消息

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

可以这样问 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

  1. -u flag: Forces Python to run in unbuffered mode, ensuring output (including errors) is flushed immediately rather than held in buffers that may be lost on crash
  2. 2>&1: Redirects stderr to stdout so both streams are captured together
  3. head: 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 -u flag 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

这个技能有用吗?

0

登录 后即可投票。

评论

  • …

同一仓库的更多技能

相似技能