EADST

Debugging with Python PDB

Debugging is an essential part of software development, and Python’s built-in PDB (Python Debugger) is a powerful tool for this task. In this post, we'll cover how to use PDB to inspect your code and fix bugs.

What is PDB?

PDB is a debugger that allows you to pause your Python program, inspect variables, and step through code interactively. It’s part of Python’s standard library, so no installation is needed.

Setting Up PDB

To use PDB, simply import it and set a breakpoint using pdb.set_trace(). Here's an example:

import pdb

def divide(a, b):
    pdb.set_trace()  # Breakpoint
    return a / b

result = divide(10, 2)
print(result)

When the program reaches pdb.set_trace(), it will pause and give you control in the debugger.

Basic Commands

While in PDB, you can use commands to control the flow of execution:

  • n: Execute the next line of code.
  • s: Step into a function call.
  • c: Continue execution until the next breakpoint.
  • p <variable>: Print the value of a variable.
  • q: Quit the debugger and stop the program.

Example:

(Pdb) p a  # Print value of 'a'
(Pdb) n    # Execute next line
(Pdb) c    # Continue execution
(Pdb) q    # Quit the debugger

Conclusion

PDB is a simple yet powerful tool to help debug your Python code. By setting breakpoints, inspecting variables, stepping through code, and quitting the debugger when done, you can easily identify and fix issues. Happy debugging!

相关标签
About Me
XD
Goals determine what you are going to be.
Category
标签云
XML 算法题 阿里云 公式 git C++ 图形思考法 printf 签证 Vim Knowledge FP16 净利润 Zip git-lfs BeautifulSoup Base64 CV Rebuttal GoogLeNet Video 域名 InvalidArgumentError Translation Paddle 多线程 QWEN Quantization News 关于博主 Disk VGG-16 Pandas ChatGPT TSV 飞书 Paper logger WebCrawler Template 继承 Git GPTQ Color Proxy CLAP Bert Image2Text Gemma UNIX Jupyter FP32 Tensor Jetson GPT4 OpenAI AI diffusers Tiktoken Streamlit LLAMA SAM Hungarian TensorFlow GIT Agent BF16 Use Math Permission Hilton Linux TTS Website 报税 Augmentation Shortcut 论文速读 Input PIP hf DeepSeek IndexTTS2 Pillow NLTK Card Clash v2ray Heatmap Transformers OpenCV Breakpoint 证件照 LoRA Ubuntu NLP CC Pickle Github Vmess LLM FlashAttention Algorithm 递归学习法 Bipartite Docker GGML uWSGI EXCEL transformers torchinfo Excel FP8 腾讯云 VPN 版权 Mixtral CEIR Web LaTeX HuggingFace FP64 Anaconda Ptyhon Interview HaggingFace DeepStream Datetime 音频 Cloudreve API Bitcoin Quantize CTC Michelin Claude Distillation mmap Bin FastAPI Animate 顶会 llama.cpp ONNX Google Logo Land 云服务器 Llama Diagram COCO RGB Domain 财报 UI CSV Conda PDF SQL Magnet Miniforge uwsgi VSCode TensorRT Review Numpy v0.dev 第一性原理 Sklearn Markdown 多进程 PDB Nginx Random Search PyCharm 论文 Pytorch Windows Qwen2.5 Baidu Tracking Attention Crawler tar SPIE Statistics 图标 scipy CUDA YOLO Firewall JSON ResNet-50 Safetensors Dataset Python ModelScope Food NameSilo Qwen2 Password RAR Data Plate 搞笑 Django SVR CAM PyTorch OCR 强化学习 Qwen BTC Plotly WAN LeetCode XGBoost icon Freesound Hotel MD5 SQLite tqdm
站点统计

本站现有博文327篇,共被浏览828239

本站已经建立2534天!

热门文章
文章归档
回到顶部