python-debug-execution
Debug Python scripts with proper error surfacing and working directory verification
- License 许可
- MIT
- 所属仓库
- HKUDS/AgentPilot
- 最近更新
- 2026-11-12
- Stars
- ★ 8.0k
- Forks
- 944
01 前言 Front Matter
- name
- python-debug-execution
- description
- Debug Python scripts with proper error surfacing and working directory verification
02 资源文件 1
- 其他.skill_id36 B
03 SKILL.md 正文
Python Debug Execution Pattern
When executing Python scripts that may fail, use this pattern to surface clear error information and diagnose issues effectively.
Core Technique
1. Execute with Full Error Output
Always run Python scripts with stderr redirected to stdout and echo the exit code:
python3 script.py 2>&1 ; echo Exit code: $?
Why this works:
2>&1captures both stdout and stderr, ensuring tracebacks are visibleecho Exit code: $?reveals the actual exit status for debugging- Avoids opaque "command failed" errors that hide the real cause
2. Verify Working Directory in Script
Before any file operations in Python scripts, add working directory verification:
import os
# At the start of your script or before file operations
print(f"Current working directory: {os.getcwd()}")
# For debugging, also list directory contents
print(f"Directory contents: {os.listdir('.')}")
Why this works:
- File not found errors often stem from incorrect working directory assumptions
- Makes path-related failures immediately diagnosable
- Confirms the execution context matches expectations
Complete Debugging Workflow
Step 1: Add Diagnostic Code to Script
#!/usr/bin/env python3
import os
import sys
def main():
# Diagnostic: verify execution context
print(f"Working directory: {os.getcwd()}")
print(f"Python version: {sys.version}")
print(f"Directory listing: {os.listdir('.')}")
# Your actual logic here
# ...
if __name__ == "__main__":
main()
Step 2: Execute with Full Error Capture
python3 script.py 2>&1 ; echo Exit code: $?
Step 3: Analyze Output
Look for:
- Traceback messages (indicate code errors)
- Exit code (0 = success, non-zero = failure)
- Working directory confirmation
- Missing file/directory errors
Common Failure Patterns
| Symptom | Likely Cause | Debug Clue |
|---|---|---|
| FileNotFoundError | Wrong working directory | Check os.getcwd() output |
| ModuleNotFoundError | Missing dependencies | Traceback shows import path |
| PermissionError | File access issues | Traceback shows file path |
| Silent failure (exit 0, no output) | Logic bug, not crash | Add print statements |
When to Use This Pattern
- Running Python scripts in automated/agent contexts
- Debugging scripts that interact with files
- Troubleshooting CI/CD pipeline failures
- Any scenario where script output may be truncated or hidden
04 同仓库更多 Skills
agentpilot
Set up AgentPilot and use its single entry to prepare missing inputs and run configured RSI experiments. Use when the user requests AgentPilot installation or work with AgentPilot.
remember
Review auto-memory entries and propose promotions to OPENSPACE.md, OPENSPACE.local.md, or retained auto-memory topics; detect duplicates, stale facts, conflicts, and ambiguous entries before making changes.
skill-discovery
Search reusable OpenSpace skills locally, and browse cloud packages step by step when a cloud skill may help. Reusing proven skills saves tokens, improves reliability, and extends your capabilities beyond built-in tools.
delegate-task
Delegate tasks to OpenSpace — a full-stack autonomous worker for coding, DevOps, web research, and desktop automation, backed by an extensive MCP tool and skill library. Skills auto-improve through use, reducing token consumption over time. A cloud community lets agents share and collectively evolve reusable skills.
write-special-chars-ts-check
Workaround for write_file failures caused by special characters (apostrophes, backticks, template literals) using a Python heredoc script, plus a reliable TypeScript type-checking step that filters output to only the newly-written files to avoid being misled by pre-existing errors.
write-special-chars-readback
Workaround for write_file failures caused by special characters (apostrophes, backticks, template literals) using a Python heredoc script, with mandatory post-write verification via read_file and cleanup of /tmp helper scripts.