Table of Contents
1. Debugging python
1.1 Using ipdb
1.2 Common commands
Home Backend Development Python Tutorial How to use the python debugging module ipdb

How to use the python debugging module ipdb

Apr 19, 2023 am 11:25 AM
python

1. Debugging python

ipdb is a module used for interactive debugging in python. It can be installed directly using pip;

its function is similar to the python console in pycharm,
The advantage of using ipdb is to debug directly in the code,
avoiding the need to go to the python console or reset some simple variables.

How to use the python debugging module ipdb

pip install ipdb
Copy after login

1.1 Using ipdb

When the program runs to ipdb.set_trace(), it will automatically enter debug mode.

for i in range(5):
    print(i)
    ipdb.set_trace()
Copy after login

1.2 Common commands

n→ \to→next
ENTER→ \to→Repeat the last command
q→ \to→Exit
pc→ \to→Continue
l→ \to→Find where you are currently located
s→ \to→Enter subroutine
r→ \to→Run until End of subroutine

命令式
上面的方法很方便,但是也有不灵活的缺点。对于一段比较棘手的代码,我们可能需要按步执行,边运行边跟踪代码流并进行调试,这时候使用交互式的命令式调试方法更加有效。启动IPDB调试环境的方法也很简单:

python -m ipdb your_code.py
常用命令
IPDB调试环境提供的常见命令有:

帮助
使用h即可调出IPDB的帮助。可以使用help command的方法查询特定命令的具体用法。

下一条语句
使用n(next)执行下一条语句。注意一个函数调用也是一个语句。如何能够实现类似“进入函数内部”的功能呢?

进入函数内部
使用s(step into)进入函数调用的内部。

打断点
使用b line_number(break)的方式给指定的行号位置加上断点。使用b file_name:line_number的方法给指定的文件(还没执行到的代码可能在外部文件中)中指定行号位置打上断点。

另外,打断点还支持指定条件下进入,可以查询帮助文档。

一直执行直到遇到下一个断点
使用c(continue)执行代码直到遇到某个断点或程序执行完毕。

一直执行直到返回
使用r(return)执行代码直到当前所在的这个函数返回。

跳过某段代码
使用j line_number(jump)可以跳过某段代码,直接执行指定行号所在的代码。

更多上下文
在IPDB调试环境中,默认只显示当前执行的代码行,以及其上下各一行的代码。如果想要看到更多的上下文代码,可以使用l first[, second](list)命令。

其中first指示向上最多显示的行号,second指示向下最多显示的行号(可以省略)。当second小于first时,second指的是从first开始的向下的行数(相对值vs绝对值)。

根据SO上的这个问题,你还可以修改IPDB的源码,一劳永逸地改变上下文的行数。

我在哪里
调试兴起,可能你会忘了自己目前所在的行号。例如在打印了若干变量值后,屏幕完全被这些值占据。使用w或者where可以打印出目前所在的行号位置以及上下文信息。

这是啥
我们可以使用whatis variable_name的方法,查看变量的类别(感觉有点鸡肋,用type也可以办到)。

列出当前函数的全部参数
当你身处一个函数内部的时候,可以使用a(argument)打印出传入函数的所有参数的值。

打印
使用p(print)和pp(pretty print)可以打印表达式的值。

清除断点
使用cl或者clear file:line_number清除断点。如果没有参数,则清除所有断点。

再来一次
使用restart重新启动调试器,断点等信息都会保留。restart实际是run的别名,使用run args的方式传入参数。

退出
使用q退出调试,并清除所有信息。

当然,这并不是IPDB的全部。其他的命令还请参照帮助文档。文档在手,天下我有!
Copy after login

The above is the detailed content of How to use the python debugging module ipdb. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

Choosing Between PHP and Python: A Guide Choosing Between PHP and Python: A Guide Apr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Python: A Deep Dive into Their History PHP and Python: A Deep Dive into Their History Apr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Python vs. JavaScript: The Learning Curve and Ease of Use Python vs. JavaScript: The Learning Curve and Ease of Use Apr 16, 2025 am 12:12 AM

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Can vs code run in Windows 8 Can vs code run in Windows 8 Apr 15, 2025 pm 07:24 PM

VS Code can run on Windows 8, but the experience may not be great. First make sure the system has been updated to the latest patch, then download the VS Code installation package that matches the system architecture and install it as prompted. After installation, be aware that some extensions may be incompatible with Windows 8 and need to look for alternative extensions or use newer Windows systems in a virtual machine. Install the necessary extensions to check whether they work properly. Although VS Code is feasible on Windows 8, it is recommended to upgrade to a newer Windows system for a better development experience and security.

How to run sublime code python How to run sublime code python Apr 16, 2025 am 08:48 AM

To run Python code in Sublime Text, you need to install the Python plug-in first, then create a .py file and write the code, and finally press Ctrl B to run the code, and the output will be displayed in the console.

Can visual studio code be used in python Can visual studio code be used in python Apr 15, 2025 pm 08:18 PM

VS Code can be used to write Python and provides many features that make it an ideal tool for developing Python applications. It allows users to: install Python extensions to get functions such as code completion, syntax highlighting, and debugging. Use the debugger to track code step by step, find and fix errors. Integrate Git for version control. Use code formatting tools to maintain code consistency. Use the Linting tool to spot potential problems ahead of time.

Where to write code in vscode Where to write code in vscode Apr 15, 2025 pm 09:54 PM

Writing code in Visual Studio Code (VSCode) is simple and easy to use. Just install VSCode, create a project, select a language, create a file, write code, save and run it. The advantages of VSCode include cross-platform, free and open source, powerful features, rich extensions, and lightweight and fast.

See all articles