一步一步调试和IPython

时间:2023-01-24 16:35:27

From what I have read, there are two ways to debug code in Python:

根据我的阅读,有两种方法可以调试Python中的代码:

  • With a traditional debugger such as pdb or ipdb. This supports commands such as c for continue, n for step-over, s for step-into etc.), but you don't have direct access to an IPython shell which can be extremely useful for object inspection.

    使用传统的调试器,如pdb或ipdb。这支持像c这样的命令继续,n用于步进,s用于步进等等,但是你不能直接访问一个IPython shell,它对于对象检查非常有用。

  • Using IPython by embedding an IPython shell in your code. You can do from ipython import embed, and then use embed() in your code. When your program/script hits an embed() statement, you are dropped into an IPython shell. This allows the full inspection of objects and testing of Python code using all the IPython goodies. However, when using embed() you can't step-by-step through the code anymore with handy keyboard shortcuts.

    通过在代码中嵌入一个IPython shell来使用IPython。您可以从ipython导入embed,然后在代码中使用embed()。当程序/脚本遇到embed()语句时,将被放入IPython shell中。这允许使用所有IPython的优点对对象进行全面检查并测试Python代码。但是,在使用embed()时,您不能再使用方便的快捷键一步一步地通过代码。

Is there any way to combine the best of both worlds? I.e.

有没有办法把这两个世界的优点结合起来?即。

  1. Be able to step-by-step through your code with handy pdb/ipdb keyboard shortcuts.
  2. 能够通过方便的pdb/ipdb键盘快捷键一步一步地完成代码。
  3. At any such step (e.g. on a given statement), have access to a full-fledged IPython shell.
  4. 在任何这样的步骤(例如在给定的语句中),都可以访问一个完整的IPython shell。

IPython debugging as in MATLAB:

An example of this type of "enhanced debugging" can be found in MATLAB, where the user always has full access to the MATLAB engine/shell, and she can still step-by-step through her code, define conditional breakpoints, etc. From what I have discussed with other users, this is the debugging feature that people miss the most when moving from MATLAB to IPython.

这种类型的一个例子可以找到“加强调试”在MATLAB,用户总是完全访问MATLAB引擎/壳,她仍然可以一步一步通过代码,定义条件断点,等等。我已经讨论了与其他用户,这是调试特性,人们最当从MATLAB IPython小姐。

IPython debugging in Emacs and other editors:

I don't want to make the question too specific, but I work mostly in Emacs, so I wonder if there is any way to bring this functionality into it. Ideally, Emacs (or the editor) would allow the programmer to set breakpoints anywhere on the code and communicate with the interpreter or debugger to have it stop in the location of your choice, and bring to a full IPython interpreter on that location.

我不想把这个问题说得太具体,但我主要在Emacs中工作,所以我想知道是否有办法将这个功能引入其中。理想情况下,Emacs(或编辑器)允许程序员在代码的任何地方设置断点,并与解释器或调试器通信,使其在您选择的位置停止,并将其带到该位置上的完整IPython解释器。

13 个解决方案

#1


74  

What about ipdb.set_trace() ? In your code :

关于ipdb.set_trace()?在你的代码:

import ipdb; ipdb.set_trace()

进口ipdb;ipdb.set_trace()

This allows for full inspection of your code, and you have access to commands such as c (continue), n (execute next line), s (step into the method at point) and so on.

这允许对代码进行全面检查,您可以访问诸如c (continue)、n (execute next line)、s (step into the method at point)等命令。

See the ipdb repo and a list of commands. IPython is now called (edit: part of) Jupyter.

请参阅ipdb repo和一个命令列表。IPython现在被称为(编辑:部分)Jupyter。


ps: note that an ipdb command takes precedence over python code. So in order to write list(foo) you'd need print list(foo).

注意,ipdb命令优先于python代码。为了写出list(foo)你需要print list(foo)

Also, if you like the ipython prompt (its emacs and vim modes, history, completions,…) it's easy to get the same for your project since it's based on the python prompt toolkit.

另外,如果您喜欢ipython提示符(它的emacs和vim模式、历史、完成等),那么您的项目很容易得到相同的结果,因为它是基于python提示工具包的。

#2


40  

(Update on May 28, 2016) Using RealGUD in Emacs

For anyone in Emacs, this thread shows how to accomplish everything described in the OP (and more) using

对于Emacs中的任何人来说,这个线程显示了如何使用OP(以及更多)描述的所有内容

  1. a new important debugger in Emacs called RealGUD which can operate with any debugger (including ipdb).
  2. Emacs中的一个新的重要调试器,名为RealGUD,它可以使用任何调试器(包括ipdb)进行操作。
  3. The Emacs package isend-mode.
  4. isend-mode Emacs包。

The combination of these two packages is extremely powerful and allows one to recreate exactly the behavior described in the OP and do even more.

这两个包的组合非常强大,允许您重新创建OP中描述的行为,并做更多事情。

More info on the wiki article of RealGUD for ipdb.

更多关于ipdb RealGUD的wiki文章的信息。


Original answer:

After having tried many different methods for debugging Python, including everything mentioned in this thread, one of my preferred ways of debugging Python with IPython is with embedded shells.

在尝试了许多调试Python的方法(包括这个线程中提到的所有方法)之后,我最喜欢的使用IPython调试Python的方法之一是使用嵌入式shell。

Defining a custom embedded IPython shell:

Add the following on a script to your PYTHONPATH, so that the method ipsh() becomes available.

将下面的脚本添加到PYTHONPATH中,以便方法ipsh()变为可用。

import inspect

# First import the embed function
from IPython.terminal.embed import InteractiveShellEmbed
from IPython.config.loader import Config

# Configure the prompt so that I know I am in a nested (embedded) shell
cfg = Config()
prompt_config = cfg.PromptManager
prompt_config.in_template = 'N.In <\\#>: '
prompt_config.in2_template = '   .\\D.: '
prompt_config.out_template = 'N.Out<\\#>: '

# Messages displayed when I drop into and exit the shell.
banner_msg = ("\n**Nested Interpreter:\n"
"Hit Ctrl-D to exit interpreter and continue program.\n"
"Note that if you use %kill_embedded, you can fully deactivate\n"
"This embedded instance so it will never turn on again")   
exit_msg = '**Leaving Nested interpreter'

# Wrap it in a function that gives me more context:
def ipsh():
    ipshell = InteractiveShellEmbed(config=cfg, banner1=banner_msg, exit_msg=exit_msg)

    frame = inspect.currentframe().f_back
    msg   = 'Stopped at {0.f_code.co_filename} at line {0.f_lineno}'.format(frame)

    # Go back one level! 
    # This is needed because the call to ipshell is inside the function ipsh()
    ipshell(msg,stack_depth=2)

Then, whenever I want to debug something in my code, I place ipsh() right at the location where I need to do object inspection, etc. For example, say I want to debug my_function below

然后,每当我想在代码中调试什么东西时,我就把ipsh()放在需要进行对象检查的位置,等等

Using it:

def my_function(b):
  a = b
  ipsh() # <- This will embed a full-fledged IPython interpreter
  a = 4

and then I invoke my_function(2) in one of the following ways:

然后我以以下方式调用my_function(2):

  1. Either by running a Python program that invokes this function from a Unix shell
  2. 或者通过运行从Unix shell调用此函数的Python程序
  3. Or by invoking it directly from IPython
  4. 或者直接从IPython调用它

Regardless of how I invoke it, the interpreter stops at the line that says ipsh(). Once you are done, you can do Ctrl-D and Python will resume execution (with any variable updates that you made). Note that, if you run the code from a regular IPython the IPython shell (case 2 above), the new IPython shell will be nested inside the one from which you invoked it, which is perfectly fine, but it's good to be aware of. Eitherway, once the interpreter stops on the location of ipsh, I can inspect the value of a (which be 2), see what functions and objects are defined, etc.

不管我如何调用它,解释器都会在表示ipsh()的行上停止。完成之后,可以按Ctrl-D, Python将继续执行(进行任何变量更新)。请注意,如果您从一个常规的IPython(上面的例子2)中运行代码,那么新的IPython shell将嵌套在您调用它的那个内,这是完全可以的,但是要知道它是好的。另外,一旦解释器停在ipsh的位置,我可以检查a的值(即2),查看定义了什么函数和对象,等等。

The problem:

The solution above can be used to have Python stop anywhere you want in your code, and then drop you into a fully-fledged IPython interpreter. Unfortunately it does not let you add or remove breakpoints once you invoke the script, which is highly frustrating. In my opinion, this is the only thing that is preventing IPython from becoming a great debugging tool for Python.

上面的解决方案可以用于在代码中让Python停止您想要的任何位置,然后将您放置到成熟的IPython解释器中。不幸的是,在调用脚本之后,它不允许您添加或删除断点,这非常令人沮丧。在我看来,这是阻止IPython成为Python优秀的调试工具的唯一原因。

The best you can do for now:

A workaround is to place ipsh() a priori at the different locations where you want the Python interpreter to launch an IPython shell (i.e. a breakpoint). You can then "jump" between different pre-defined, hard-coded "breakpoints" with Ctrl-D, which would exit the current embedded IPython shell and stop again whenever the interpreter hits the next call to ipsh().

解决方案是将ipsh()预先放置在希望Python解释器启动IPython shell(即断点)的不同位置。然后,您可以使用Ctrl-D在不同的预定义、硬编码的“断点”之间“跳转”,它将退出当前嵌入的IPython shell,并在解释器下一次调用ipsh()时再次停止。

If you go this route, one way to exit "debugging mode" and ignore all subsequent breakpoints, is to use ipshell.dummy_mode = True which will make Python ignore any subsequent instantiations of the ipshell object that we created above.

如果您使用此路径,退出“调试模式”并忽略所有后续断点的一种方法是使用ipshell。dummy_mode = True将使Python忽略前面创建的ipshell对象的任何后续实例化。

#3


18  

You can start IPython session from pudb and go back to the debugging session as you like.

您可以从pudb开始IPython会话,然后按照自己的意愿返回到调试会话。

BTW, ipdb is using IPython behind the scenes and you can actually use IPython functionality such as TAB completion and magic commands (the one starts with %). If you are OK with ipdb you can start it from IPython using commands such as %run and %debug. ipdb session is actually better than plain IPython one in the sense you can go up and down in the stack trace etc. What is missing in ipdb for "object inspection"?

BTW, ipdb在幕后使用IPython,您可以实际使用IPython功能,比如制表符完成和魔法命令(从%开始)。如果您对ipdb没有问题,您可以使用%run和%debug等命令从IPython启动它。ipdb会话实际上比普通的IPython会话更好,因为您可以在堆栈跟踪中上下移动。

Also, python.el bundled with Emacs >= 24.3 has nice ipdb support.

同时,python。el与Emacs >= 24.3绑定,具有良好的ipdb支持。

#4


10  

Nobody mentioned IPython's %pdb flag yet. Just call %pdb in IPython and when an error occurs, you're automatically dropped to ipdb. While you don't have the stepping immediately, you're in ipdb afterwards.

还没有人提到IPython的%pdb标志。只需在IPython中调用%pdb,当出现错误时,您将自动切换到ipdb。虽然您没有立即执行步骤,但您随后将进入ipdb。

This makes debugging individual functions easy, as you can just load a file with %load and then run a function. You could force an error with an assert at the right position.

这使得调试单个函数变得很容易,因为您只需加载带有%load的文件,然后运行一个函数。您可以在正确的位置使用断言强制执行错误。

#5


7  

Looks like the approach in @gaborous's answer is deprecated.

看起来@gaborous的方法已经过时了。

The new approach seems to be:

新的办法似乎是:

from IPython.core import debugger
debug = debugger.Pdb().set_trace

def buggy_method():
    debug()

#6


6  

Prefixing an "!" symbol to commands you type in pdb seems to have the same effect as doing something in an IPython shell. This works for accessing help for a certain function, or even variable names. Maybe this will help you to some extent. For example,

在pdb中输入的命令前面加上“!”符号,似乎与在IPython shell中执行某些操作具有相同的效果。该方法用于访问特定函数的帮助,甚至是变量名。也许这在某种程度上会对你有所帮助。例如,

ipdb> help(numpy.transpose)
*** No help on (numpy.transpose)

But !help(numpy.transpose) will give you the expected help page on numpy.transpose. Similarly for variable names, say you have a variable l, typing "l" in pdb lists the code, but !l prints the value of l.

但是!help(numpi .转置)将给出关于numpi .转置的期望帮助页面。类似地,对于变量名,假设您有一个变量l,在pdb中键入“l”会列出代码,但是,我输出l的值。

#7


4  

Did you try this tip?

你试过这个方法吗?

Or better still, use ipython, and call:

或者更好的是,使用ipython调用:

from IPython.Debugger import Tracer; debug_here = Tracer()

then you can just use

然后你就可以用了

debug_here()

whenever you want to set a breakpoint

无论何时您想要设置断点

#8


2  

If you type exit() in embed() console the code continue and go to the next embed() line.

如果在embed()中键入exit(),则代码将继续,并进入下一个embed()行。

#9


2  

The Pyzo IDE has similar capabilities as the OP asked for. You don't have to start in debug mode. Similarly to MATLAB, the commands are executed in the shell. When you set up a break-point in some source code line, the IDE stops the execution there and you can debug and issue regular IPython commands as well.

Pyzo IDE具有与OP所要求的类似的功能。您不必从调试模式开始。与MATLAB类似,命令在shell中执行。当您在某个源代码行中设置断点时,IDE将停止执行,您还可以调试并发出常规的IPython命令。

It does seem however that step-into doesn't (yet?) work well (i.e. stopping in one line and then stepping into another function) unless you set up another break-point.

然而,除非你设置了另一个断点,否则step-into并没有很好地工作(例如,在一行中停下来,然后进入另一个函数)。

Still, coming from MATLAB, this seems the best solution I've found.

不过,从MATLAB来看,这似乎是我找到的最好的解决方案。

#10


2  

One option is to use an IDE like Spyder which should allow you to interact with your code while debugging (using an IPython console, in fact). In fact, Spyder is very MATLAB-like, which I presume was intentional. That includes variable inspectors, variable editing, built-in access to documentation, etc.

一种选择是使用像Spyder这样的IDE,它应该允许您在调试时与代码进行交互(实际上使用的是IPython控制台)。事实上,Spyder很像matlablike,我认为这是故意的。这包括变量检查器、变量编辑、内置文档访问等。

#11


1  

Running from inside Emacs' IPython-shell and breakpoint set via pdb.set_trace() should work.

通过pdb.set_trace()从Emacs的IPython-shell和断点设置中运行。

Checked with python-mode.el, M-x ipython RET etc.

与python-mode检查。el, M-x ipython RET等。

#12


1  

From python 3.2, you have the interact command, which gives you access to the full python/ipython command space.

在python 3.2中,您有了交互命令,它允许您访问完整的python/ipython命令空间。

#13


1  

the right, easy, cool, exact answer for the question is to use %run macro with -d flag.

正确、简单、酷、准确的答案是使用带有-d标志的%run宏。

In [4]: run -d myscript.py
NOTE: Enter 'c' at the ipdb>  prompt to continue execution.        
> /cygdrive/c/Users/mycodefolder/myscript.py(4)<module>()
      2                                                            
      3                        
----> 4 a=1                                            
      5 b=2

#1


74  

What about ipdb.set_trace() ? In your code :

关于ipdb.set_trace()?在你的代码:

import ipdb; ipdb.set_trace()

进口ipdb;ipdb.set_trace()

This allows for full inspection of your code, and you have access to commands such as c (continue), n (execute next line), s (step into the method at point) and so on.

这允许对代码进行全面检查,您可以访问诸如c (continue)、n (execute next line)、s (step into the method at point)等命令。

See the ipdb repo and a list of commands. IPython is now called (edit: part of) Jupyter.

请参阅ipdb repo和一个命令列表。IPython现在被称为(编辑:部分)Jupyter。


ps: note that an ipdb command takes precedence over python code. So in order to write list(foo) you'd need print list(foo).

注意,ipdb命令优先于python代码。为了写出list(foo)你需要print list(foo)

Also, if you like the ipython prompt (its emacs and vim modes, history, completions,…) it's easy to get the same for your project since it's based on the python prompt toolkit.

另外,如果您喜欢ipython提示符(它的emacs和vim模式、历史、完成等),那么您的项目很容易得到相同的结果,因为它是基于python提示工具包的。

#2


40  

(Update on May 28, 2016) Using RealGUD in Emacs

For anyone in Emacs, this thread shows how to accomplish everything described in the OP (and more) using

对于Emacs中的任何人来说,这个线程显示了如何使用OP(以及更多)描述的所有内容

  1. a new important debugger in Emacs called RealGUD which can operate with any debugger (including ipdb).
  2. Emacs中的一个新的重要调试器,名为RealGUD,它可以使用任何调试器(包括ipdb)进行操作。
  3. The Emacs package isend-mode.
  4. isend-mode Emacs包。

The combination of these two packages is extremely powerful and allows one to recreate exactly the behavior described in the OP and do even more.

这两个包的组合非常强大,允许您重新创建OP中描述的行为,并做更多事情。

More info on the wiki article of RealGUD for ipdb.

更多关于ipdb RealGUD的wiki文章的信息。


Original answer:

After having tried many different methods for debugging Python, including everything mentioned in this thread, one of my preferred ways of debugging Python with IPython is with embedded shells.

在尝试了许多调试Python的方法(包括这个线程中提到的所有方法)之后,我最喜欢的使用IPython调试Python的方法之一是使用嵌入式shell。

Defining a custom embedded IPython shell:

Add the following on a script to your PYTHONPATH, so that the method ipsh() becomes available.

将下面的脚本添加到PYTHONPATH中,以便方法ipsh()变为可用。

import inspect

# First import the embed function
from IPython.terminal.embed import InteractiveShellEmbed
from IPython.config.loader import Config

# Configure the prompt so that I know I am in a nested (embedded) shell
cfg = Config()
prompt_config = cfg.PromptManager
prompt_config.in_template = 'N.In <\\#>: '
prompt_config.in2_template = '   .\\D.: '
prompt_config.out_template = 'N.Out<\\#>: '

# Messages displayed when I drop into and exit the shell.
banner_msg = ("\n**Nested Interpreter:\n"
"Hit Ctrl-D to exit interpreter and continue program.\n"
"Note that if you use %kill_embedded, you can fully deactivate\n"
"This embedded instance so it will never turn on again")   
exit_msg = '**Leaving Nested interpreter'

# Wrap it in a function that gives me more context:
def ipsh():
    ipshell = InteractiveShellEmbed(config=cfg, banner1=banner_msg, exit_msg=exit_msg)

    frame = inspect.currentframe().f_back
    msg   = 'Stopped at {0.f_code.co_filename} at line {0.f_lineno}'.format(frame)

    # Go back one level! 
    # This is needed because the call to ipshell is inside the function ipsh()
    ipshell(msg,stack_depth=2)

Then, whenever I want to debug something in my code, I place ipsh() right at the location where I need to do object inspection, etc. For example, say I want to debug my_function below

然后,每当我想在代码中调试什么东西时,我就把ipsh()放在需要进行对象检查的位置,等等

Using it:

def my_function(b):
  a = b
  ipsh() # <- This will embed a full-fledged IPython interpreter
  a = 4

and then I invoke my_function(2) in one of the following ways:

然后我以以下方式调用my_function(2):

  1. Either by running a Python program that invokes this function from a Unix shell
  2. 或者通过运行从Unix shell调用此函数的Python程序
  3. Or by invoking it directly from IPython
  4. 或者直接从IPython调用它

Regardless of how I invoke it, the interpreter stops at the line that says ipsh(). Once you are done, you can do Ctrl-D and Python will resume execution (with any variable updates that you made). Note that, if you run the code from a regular IPython the IPython shell (case 2 above), the new IPython shell will be nested inside the one from which you invoked it, which is perfectly fine, but it's good to be aware of. Eitherway, once the interpreter stops on the location of ipsh, I can inspect the value of a (which be 2), see what functions and objects are defined, etc.

不管我如何调用它,解释器都会在表示ipsh()的行上停止。完成之后,可以按Ctrl-D, Python将继续执行(进行任何变量更新)。请注意,如果您从一个常规的IPython(上面的例子2)中运行代码,那么新的IPython shell将嵌套在您调用它的那个内,这是完全可以的,但是要知道它是好的。另外,一旦解释器停在ipsh的位置,我可以检查a的值(即2),查看定义了什么函数和对象,等等。

The problem:

The solution above can be used to have Python stop anywhere you want in your code, and then drop you into a fully-fledged IPython interpreter. Unfortunately it does not let you add or remove breakpoints once you invoke the script, which is highly frustrating. In my opinion, this is the only thing that is preventing IPython from becoming a great debugging tool for Python.

上面的解决方案可以用于在代码中让Python停止您想要的任何位置,然后将您放置到成熟的IPython解释器中。不幸的是,在调用脚本之后,它不允许您添加或删除断点,这非常令人沮丧。在我看来,这是阻止IPython成为Python优秀的调试工具的唯一原因。

The best you can do for now:

A workaround is to place ipsh() a priori at the different locations where you want the Python interpreter to launch an IPython shell (i.e. a breakpoint). You can then "jump" between different pre-defined, hard-coded "breakpoints" with Ctrl-D, which would exit the current embedded IPython shell and stop again whenever the interpreter hits the next call to ipsh().

解决方案是将ipsh()预先放置在希望Python解释器启动IPython shell(即断点)的不同位置。然后,您可以使用Ctrl-D在不同的预定义、硬编码的“断点”之间“跳转”,它将退出当前嵌入的IPython shell,并在解释器下一次调用ipsh()时再次停止。

If you go this route, one way to exit "debugging mode" and ignore all subsequent breakpoints, is to use ipshell.dummy_mode = True which will make Python ignore any subsequent instantiations of the ipshell object that we created above.

如果您使用此路径,退出“调试模式”并忽略所有后续断点的一种方法是使用ipshell。dummy_mode = True将使Python忽略前面创建的ipshell对象的任何后续实例化。

#3


18  

You can start IPython session from pudb and go back to the debugging session as you like.

您可以从pudb开始IPython会话,然后按照自己的意愿返回到调试会话。

BTW, ipdb is using IPython behind the scenes and you can actually use IPython functionality such as TAB completion and magic commands (the one starts with %). If you are OK with ipdb you can start it from IPython using commands such as %run and %debug. ipdb session is actually better than plain IPython one in the sense you can go up and down in the stack trace etc. What is missing in ipdb for "object inspection"?

BTW, ipdb在幕后使用IPython,您可以实际使用IPython功能,比如制表符完成和魔法命令(从%开始)。如果您对ipdb没有问题,您可以使用%run和%debug等命令从IPython启动它。ipdb会话实际上比普通的IPython会话更好,因为您可以在堆栈跟踪中上下移动。

Also, python.el bundled with Emacs >= 24.3 has nice ipdb support.

同时,python。el与Emacs >= 24.3绑定,具有良好的ipdb支持。

#4


10  

Nobody mentioned IPython's %pdb flag yet. Just call %pdb in IPython and when an error occurs, you're automatically dropped to ipdb. While you don't have the stepping immediately, you're in ipdb afterwards.

还没有人提到IPython的%pdb标志。只需在IPython中调用%pdb,当出现错误时,您将自动切换到ipdb。虽然您没有立即执行步骤,但您随后将进入ipdb。

This makes debugging individual functions easy, as you can just load a file with %load and then run a function. You could force an error with an assert at the right position.

这使得调试单个函数变得很容易,因为您只需加载带有%load的文件,然后运行一个函数。您可以在正确的位置使用断言强制执行错误。

#5


7  

Looks like the approach in @gaborous's answer is deprecated.

看起来@gaborous的方法已经过时了。

The new approach seems to be:

新的办法似乎是:

from IPython.core import debugger
debug = debugger.Pdb().set_trace

def buggy_method():
    debug()

#6


6  

Prefixing an "!" symbol to commands you type in pdb seems to have the same effect as doing something in an IPython shell. This works for accessing help for a certain function, or even variable names. Maybe this will help you to some extent. For example,

在pdb中输入的命令前面加上“!”符号,似乎与在IPython shell中执行某些操作具有相同的效果。该方法用于访问特定函数的帮助,甚至是变量名。也许这在某种程度上会对你有所帮助。例如,

ipdb> help(numpy.transpose)
*** No help on (numpy.transpose)

But !help(numpy.transpose) will give you the expected help page on numpy.transpose. Similarly for variable names, say you have a variable l, typing "l" in pdb lists the code, but !l prints the value of l.

但是!help(numpi .转置)将给出关于numpi .转置的期望帮助页面。类似地,对于变量名,假设您有一个变量l,在pdb中键入“l”会列出代码,但是,我输出l的值。

#7


4  

Did you try this tip?

你试过这个方法吗?

Or better still, use ipython, and call:

或者更好的是,使用ipython调用:

from IPython.Debugger import Tracer; debug_here = Tracer()

then you can just use

然后你就可以用了

debug_here()

whenever you want to set a breakpoint

无论何时您想要设置断点

#8


2  

If you type exit() in embed() console the code continue and go to the next embed() line.

如果在embed()中键入exit(),则代码将继续,并进入下一个embed()行。

#9


2  

The Pyzo IDE has similar capabilities as the OP asked for. You don't have to start in debug mode. Similarly to MATLAB, the commands are executed in the shell. When you set up a break-point in some source code line, the IDE stops the execution there and you can debug and issue regular IPython commands as well.

Pyzo IDE具有与OP所要求的类似的功能。您不必从调试模式开始。与MATLAB类似,命令在shell中执行。当您在某个源代码行中设置断点时,IDE将停止执行,您还可以调试并发出常规的IPython命令。

It does seem however that step-into doesn't (yet?) work well (i.e. stopping in one line and then stepping into another function) unless you set up another break-point.

然而,除非你设置了另一个断点,否则step-into并没有很好地工作(例如,在一行中停下来,然后进入另一个函数)。

Still, coming from MATLAB, this seems the best solution I've found.

不过,从MATLAB来看,这似乎是我找到的最好的解决方案。

#10


2  

One option is to use an IDE like Spyder which should allow you to interact with your code while debugging (using an IPython console, in fact). In fact, Spyder is very MATLAB-like, which I presume was intentional. That includes variable inspectors, variable editing, built-in access to documentation, etc.

一种选择是使用像Spyder这样的IDE,它应该允许您在调试时与代码进行交互(实际上使用的是IPython控制台)。事实上,Spyder很像matlablike,我认为这是故意的。这包括变量检查器、变量编辑、内置文档访问等。

#11


1  

Running from inside Emacs' IPython-shell and breakpoint set via pdb.set_trace() should work.

通过pdb.set_trace()从Emacs的IPython-shell和断点设置中运行。

Checked with python-mode.el, M-x ipython RET etc.

与python-mode检查。el, M-x ipython RET等。

#12


1  

From python 3.2, you have the interact command, which gives you access to the full python/ipython command space.

在python 3.2中,您有了交互命令,它允许您访问完整的python/ipython命令空间。

#13


1  

the right, easy, cool, exact answer for the question is to use %run macro with -d flag.

正确、简单、酷、准确的答案是使用带有-d标志的%run宏。

In [4]: run -d myscript.py
NOTE: Enter 'c' at the ipdb>  prompt to continue execution.        
> /cygdrive/c/Users/mycodefolder/myscript.py(4)<module>()
      2                                                            
      3                        
----> 4 a=1                                            
      5 b=2