简体   繁体   English

Python - 相当于 Matlab/Octave 键盘功能?

[英]Python - equivalent of Matlab/Octave keyboard function?

there is a function keyboard in Matlab/Octave that stops execution and gives control to the keyboard. Matlab/Octave 中有一个功能keyboard ,可以停止执行并控制键盘。

It would be useful thing to have around when I have a long running script and need to test a few things.当我有一个长时间运行的脚本并且需要测试一些东西时,它会很有用。 Since Python is interpreted language I guess it should be possible.由于 Python 是解释性语言,我想它应该是可能的。

Is there such a thing in Python or I should use something more 'Pythonic'? Python 中有这样的东西,还是我应该使用更“Pythonic”的东西? :) :)

Since Python 3.7 you can use built-in breakpoint() function, as described in PEP 553 .从 Python 3.7 开始,您可以使用内置的breakpoint()函数,如PEP 553 中所述 In previous Python versions you would use import pdb; pdb.set_trace()在以前的 Python 版本中,您将使用import pdb; pdb.set_trace() import pdb; pdb.set_trace() as the accepted answer states. import pdb; pdb.set_trace()作为接受的答案状态。

You can use a debugger, such as pdb or ipdb.您可以使用调试器,例如 pdb 或 ipdb。 Once you've acquired the module, just add the following line to your code where you want to stop it:获得模块后,只需将以下行添加到您要停止它的代码中:

import ipdb; ipdb.set_trace()

You can use these commands to move through the code once you have control.一旦获得控制权,就可以使用这些命令在代码中移动。

If you're not using an IDE, use the Python Debugger .如果您不使用 IDE,请使用Python Debugger If you are using an IDE, use your IDE's debugger.如果您使用的是 IDE,请使用 IDE 的调试器。

import code
code.interact(local=locals())

Returns IDE command prompt just like keyboard in MATLAB, whereas pdb seems to be more focused on stepping through breakpoints in the code (although I haven't used pdb much so haven't dived into much of the functionality)像 MATLAB 中的keyboard一样返回 IDE 命令提示符,而 pdb 似乎更专注于单步执行代码中的断点(虽然我没有经常使用 pdb,所以没有深入研究很多功能)

From: http://ubuntugeeknerd.blogspot.com/2013/12/matlab-keyboard-equivalent-in-python.html来自: http : //ubuntugeeknerd.blogspot.com/2013/12/matlab-keyboard-equivalent-in-python.html

Further documentation: https://www.digitalocean.com/community/tutorials/how-to-debug-python-with-an-interactive-console更多文档: https : //www.digitalocean.com/community/tutorials/how-to-debug-python-with-an-interactive-console

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM