介绍
**Debugging 是软件开发过程的一部分,程序员会寻找并解决阻止软件正确运行的问题。
一个有用的和快速的调试工具是Python code
模块,因为它可以用来模拟交互式解释器。
前提条件
如果您没有设置编程环境,您可以参考本地编程环境的安装和安装指南(https://www.digitalocean.com/community/tutorial_series/how-to-install-and-set-up-a-local-programming-environment-for-python-3)或适用于您的操作系统(Ubuntu, CentOS, Debian 等)的编程环境(https://www.digitalocean.com/community/tutorial_collections/how-to-install-python-3-and-set-up-a-programming-environment)。
理解代码
模块
相反,你可以通过代码进行调试,你可以将代码
模块(LINK0
)添加到你的Python程序中,指示该程序停止执行并进入互动模式,以检查你的代码是如何工作的。
通过使用代码
模块,您可以避免在整个代码中使用print()
陈述作为调试的一种形式,随着时间的推移而变得模糊。
要使用模块作为调试方法,您可以使用模块的interact()
函数,该函数在调用时停止执行程序,并为您提供一个交互式控制台,以便您可以检查程序的当前状态。
<$>[info]
信息: 要跟进本教程中的示例代码,请在本地系统上运行python3
命令,打开Python交互壳。
函数及其可能的参数如下:
1code.interact(banner=None, readfunc=None, local=None, exitmsg=None)
此函数运行一个读取eval-打印循环,并创建一个对象实例的 InteractiveConsole
类,模拟交互式 Python 解释器的行为。
可选参数如下:
banner
可以设置为 string,以便您可以标记导译器 launchesreadfunc
可以用作InteractiveConsole.raw_input()
方法local
将设置导译器 loopexitmsg
的默认名空间,可以设置为导译器 ends
使用本地
参数,您可以使用,例如:
local=locals()
for a local namespacelocal=globals()
for a global namespacelocal=dict(globals(), **locals())
to use both the global namespace and the present local namespace
请注意,exitmsg
参数是Python 3.6的新参数,所以如果您正在使用较旧的Python版本,请更新它或关闭exitmsg
参数。
您可以将interact()
函数放置在您想要的任何地方,在您的程序中启动代码中的交互式解释器。
使用代码
模块
让我们在一个名为balances.py
的银行账户余额程序的背景下来看看这一点,我们将本地
参数设置为本地()
,以便将名称空间设置为本地。
1[label balances.py]
2# Import code module
3import code
4
5bal_a = 2324
6bal_b = 0
7bal_c = 409
8bal_d = -2
9
10account_balances = [bal_a, bal_b, bal_c, bal_d]
11
12def display_bal():
13 for balance in account_balances:
14 if balance < 0:
15 print("Account balance of {} is below 0; add funds now."
16 .format(balance))
17
18 elif balance == 0:
19 print("Account balance of {} is equal to 0; add funds soon."
20 .format(balance))
21
22 else:
23 print("Account balance of {} is above 0.".format(balance))
24
25# Use interact() function to start the interpreter with local namespace
26code.interact(local=locals())
27
28display_bal()
我们使用函数 code.interact()
与 local=locals()
参数以使用本地名称空间作为解释器循环中的默认值。
让我们运行上述程序,如果我们不在虚拟环境中,使用python3
命令,或者如果我们在虚拟环境中,使用python
命令:
1python balances.py
一旦我们运行该程序,我们将首先收到以下输出:
1Python 3.5.2 (default, Nov 17 2016, 17:05:23)
2[GCC 5.4.0 20160609] on linux
3Type "help", "copyright", "credits" or "license" for more information.
4(InteractiveConsole)
5>>>
你的标记将被放置在>>>
行的末尾,就像它在Python交互壳中一样。
从这里,您可以发出调用来打印变量、函数等:
1>>> print(bal_c)
2409
3>>> print(account_balances)
4[2324, 0, 409, -2]
5>>> print(display_bal())
6Account balance of 2324 is 0 or above.
7Account balance of 0 is equal to 0, add funds soon.
8Account balance of 409 is 0 or above.
9Account balance of -2 is below 0, add funds now.
10None
11>>> print(display_bal)
12<function display_bal at 0x104b80f28>
13>>>
我们看到,通过使用本地名称空间,我们可以打印变量并召唤函数。
一旦你满足于你能够通过与翻译器合作检查的内容,你可以按CTRL + D
为 *nix 基于的系统,或CTRL + Z
为 Windows 基于的系统离开控制台并继续执行该程序。
如果您想离开控制台而不运行其余的程序,您可以通过键入quit()
来做到这一点,该程序将被取消。
为了利用banner
和exitmsg
参数,我们可以这样做:
1[label balances.py]
2...
3# Use interact() function to start the interpreter
4code.interact(banner="Start", local=locals(), exitmsg="End")
5
6display_bal()
当我们运行程序时,当我们运行程序时,我们将收到以下输出:
1Start
2>>>
使用banner
参数可以允许您在代码中设置多个点,并为您提供识别这些点的能力,例如,您可以有一个banner
打印In [for-loop](https://andsky.com/tech/tutorials/how-to-construct-for-loops-in-python-3)
和一个exitmsg
打印Out of for-loop
,这样您就可以准确地知道您在代码中的位置。
从这里,我们可以像往常一样使用翻译器。一旦我们键入CTRL + D
来退出翻译器,我们将收到退出消息,函数将运行:
1End
2Account balance of 2324 is 0 or above.
3Account balance of 0 is equal to 0, add funds soon.
4Account balance of 409 is 0 or above.
5Account balance of -2 is below 0, add funds now.
该计划已在交互式会议后全面运行。
一旦你完成了使用代码
模块来调试你的代码,你应该删除代码
函数和导入声明,以便你的程序将正常运行。
结论
使用代码
模块启动交互式控制台可以让您在细微层面查看代码正在做什么,以了解其行为并根据需要进行更改。
要了解有关您可以使用的其他方法来调试 Python 代码的更多信息,请阅读我们的教程 如何使用 Python 调试器 pdb
,以及我们的教程 如何使用日志 。