python断点_Python断点()

python断点

Python breakpoint() is a new built-in function introduced in Python 3.7. Python code debugging has always been a painful process because of tight coupling between the actual code and the debugging module code.

Python breakpoint()是Python 3.7中引入的新内置函数。 由于实际代码和调试模块代码之间的紧密联系,Python代码调试一直是一个痛苦的过程。

For example, if you are using pdb debugger, then you will have to call pdb.set_trace() in your program code. If you want to use any other debugger, let’s say web-pdb then you will have to remove all the code related to pdb and add web_pdb.set_trace() method. This causes a huge overhead in using python debugger and make the python code hard to debug and maintain.

例如,如果使用pdb调试器 ,则必须在程序代码中调用pdb.set_trace() 。 如果要使用任何其他调试器,例如web-pdb则必须删除所有与pdb相关的代码,并添加web_pdb.set_trace()方法。 这在使用python调试器时造成了巨大的开销,并使python代码难以调试和维护。

That’s why Python 3.7 has introduced breakpoint() method that allows us to write loosely coupled debugging code.

因此,Python 3.7引入了breakpoint()方法,该方法允许我们编写松耦合的调试代码。

Python断点() (Python breakpoint())

Python breakpoint() function calls sys.breakpointhook() function. By default, sys.breakpointhook() calls pdb.set_trace() function. So at the very least, using breakpoint() provide convinience in using a debugger because we don’t have to explicitly import pdb module.

Python breakpoint()函数调用sys.breakpointhook()函数。 默认情况下,sys.breakpointhook()调用pdb.set_trace()函数。 因此至少,使用breakpoint()可以为调试器的使用提供便利,因为我们不必显式导入pdb模块。

Let’s look at a simple example of breakpoint() function usage. We have a python script python_breakpoint_examples.py with following code.

让我们来看一个breakpoint()函数用法的简单示例。 我们有一个带有以下代码的python脚本python_breakpoint_examples.py

x = 10
y = 'Hi'
z = 'Hello'
print(y)

breakpoint()

print(z)

When we execute this script, PDB debugger console opens up.

当我们执行此脚本时,PDB调试器控制台将打开。

$python3.7 python_breakpoint_examples.py
Hi
> /Users/pankaj/Documents/PycharmProjects/BasicPython/basic_examples/python_breakpoint_examples.py(8)
   
   
    
    ()
-> print(z)
(Pdb) c
Hello
$

   
   

Python breakpoint()–停止调试 (Python breakpoint() – Stop Debugging)

Python sys.breakpointhook() function uses environment variable PYTHONBREAKPOINT to configure the debugger. If unset, default PDB debugger is used.

Python sys.breakpointhook()函数使用环境变量PYTHONBREAKPOINT来配置调试器。 如果未设置,则使用默认的PDB调试器。

If it’s set to “0” then the function returns immediately and no code debugging is performed. It’s very helpful when we want to run our code without debugging.

如果将其设置为“ 0”,则该函数将立即返回,并且不执行任何代码调试。 当我们想在不调试的情况下运行代码时,这将非常有帮助。

$PYTHONBREAKPOINT=0 python3.7 python_breakpoint_examples.py
Hi
Hello
$

Python breakpoint()–更改调试器模块 (Python breakpoint() – Change Debugger Module)

We can use PYTHONBREAKPOINT environment variable to provide the debugger method to be called by breakpoint() function. This is very helpful because we can change the debugger module easily without making any code change.

我们可以使用PYTHONBREAKPOINT环境变量来提供要由breakpoint()函数调用的调试器方法。 这非常有用,因为我们可以轻松更改调试器模块,而无需进行任何代码更改。

Let’s say we want to use web-pdb debugger. We can easily hook it into our program using PYTHONBREAKPOINT=web_pdb.set_trace.

假设我们要使用web-pdb调试器。 我们可以使用PYTHONBREAKPOINT=web_pdb.set_trace轻松将其连接到我们的程序中。

First of all, make sure that web-pdb is installed. You can install it using pip3.7 install web-pdb command.

首先,请确保已安装web-pdb。 您可以使用pip3.7 install web-pdb命令安装它。

web-pdb documentation, it’s compatible with the new breakpoint() function added in Python 3.7. web-pdb文档 ,它与Python 3.7中添加的新breakpoint()函数兼容。
$PYTHONBREAKPOINT=web_pdb.set_trace python3.7 python_breakpoint_examples.py
Hi
2018-08-10 12:49:54,339: root - web_console:110 - CRITICAL - Web-PDB: starting web-server on pankaj:5555...

Open the web-server URL provided in the console log and you will see the debugger window like below image.

打开控制台日志中提供的Web服务器URL,您将看到调试器窗口,如下图所示。

We can issue PDB commands using this UI, you can send command “c” to continue and complete our program.

我们可以使用此UI发出PDB命令,您可以发送命令“ c”继续并完成我们的程序。

摘要 (Summary)

Python breakpoint() function is a very helpful addition to python debugging feature. It’s recommended to use this for debugging so that you can easily hook other third-party debuggers on the fly. It also provides an easy option to disable debugger and runs the program normally.

Python breakpoint()函数是python调试功能的非常有用的补充。 建议使用此工具进行调试,以便您可以轻松地随时挂接其他第三方调试器。 它还提供了一个简单的选项来禁用调试器并正常运行程序。

GitHub Repository. GitHub存储库中签出更多Python示例。

Reference: Official Documentation

参考: 官方文档

翻译自: https://www.journaldev.com/22695/python-breakpoint

python断点

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值