是否可以从代码进入ipython?

发布于 2024-07-27 03:52:58 字数 79 浏览 3 评论 0原文

对于我的调试需求,pdb 非常好。 然而,如果我能进入 ipython,那就会更酷(也更有帮助)。 这件事可能吗?

For my debugging needs, pdb is pretty good. However, it would be much cooler (and helpful) if I could go into ipython. Is this thing possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(13

一世旳自豪 2024-08-03 03:52:58

有一个 ipdb 项目将 iPython 嵌入到标准 pdb 中,因此您可以这样做:

import ipdb; ipdb.set_trace()

它可以通过通常的 pip install ipdb 进行安装。

ipdb 非常短,因此除了 easy_installing,您还可以在 Python 路径上的某个位置创建一个文件 ipdb.py 并将以下内容粘贴到该文件中:

import sys
from IPython.Debugger import Pdb
from IPython.Shell import IPShell
from IPython import ipapi

shell = IPShell(argv=[''])

def set_trace():
    ip = ipapi.get()
    def_colors = ip.options.colors
    Pdb(def_colors).set_trace(sys._getframe().f_back)

There is an ipdb project which embeds iPython into the standard pdb, so you can just do:

import ipdb; ipdb.set_trace()

It's installable via the usual pip install ipdb.

ipdb is pretty short, so instead of easy_installing you can also create a file ipdb.py somewhere on your Python path and paste the following into the file:

import sys
from IPython.Debugger import Pdb
from IPython.Shell import IPShell
from IPython import ipapi

shell = IPShell(argv=[''])

def set_trace():
    ip = ipapi.get()
    def_colors = ip.options.colors
    Pdb(def_colors).set_trace(sys._getframe().f_back)
Smile简单爱 2024-08-03 03:52:58

在 IPython 0.11 中,您可以像这样将 IPython 直接嵌入到您的代码中

您的程序可能如下所示

In [5]: cat > tmpf.py
a = 1

from IPython import embed
embed() # drop into an IPython session.
        # Any variables you define or modify here
        # will not affect program execution

c = 2

^D

这就是您运行它时发生的情况(我任意选择在现有的 ipython 会话中运行它。根据我的经验,像这样嵌套 ipython 会话可以导致其崩溃)。

In [6]:

In [6]: run tmpf.py
Python 2.7.2 (default, Aug 25 2011, 00:06:33)
Type "copyright", "credits" or "license" for more information.

IPython 0.11 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]: who
a       embed

In [2]: a
Out[2]: 1

In [3]:
Do you really want to exit ([y]/n)? y


In [7]: who
a       c       embed

In IPython 0.11, you can embed IPython directly in your code like this

Your program might look like this

In [5]: cat > tmpf.py
a = 1

from IPython import embed
embed() # drop into an IPython session.
        # Any variables you define or modify here
        # will not affect program execution

c = 2

^D

This is what happens when you run it (I arbitrarily chose to run it inside an existing ipython session. Nesting ipython sessions like this in my experience can cause it to crash).

In [6]:

In [6]: run tmpf.py
Python 2.7.2 (default, Aug 25 2011, 00:06:33)
Type "copyright", "credits" or "license" for more information.

IPython 0.11 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]: who
a       embed

In [2]: a
Out[2]: 1

In [3]:
Do you really want to exit ([y]/n)? y


In [7]: who
a       c       embed
萤火眠眠 2024-08-03 03:52:58

如果您使用的是更现代版本的 IPython (> 0.10.2),您可以使用类似的内容

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

,但最好只使用 ipdb

If you're using a more modern version of IPython (> 0.10.2) you can use something like

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

But it's probably better to just use ipdb

德意的啸 2024-08-03 03:52:58

的等价物

import pdb; pdb.set_trace()

IPython

from IPython.ipapi import make_session; make_session()
from IPython.Debugger import Pdb; Pdb().set_trace()

是这样的:它有点冗长,但如果您没有安装 ipdb,最好知道。 需要一次 make_session 调用来设置配色方案等,并且 set_trace 调用可以放置在您需要中断的任何地方。

The equivalent of

import pdb; pdb.set_trace()

with IPython is something like:

from IPython.ipapi import make_session; make_session()
from IPython.Debugger import Pdb; Pdb().set_trace()

It's a bit verbose, but good to know if you don't have ipdb installed. The make_session call is required once to set up the color scheme, etc, and set_trace calls can be placed anywhere you need to break.

掐死时间 2024-08-03 03:52:58

通常,当我使用 ipython 时,我会使用其中的“pdb”命令打开自动调试。

然后,我在脚本所在的目录中使用“run myscript.py”命令运行我的脚本。

如果出现异常,ipython 会停止调试器内的程序。 查看 Magic ipython 命令的帮助命令 (%magic)

Normally, when I use ipython, I turn automatic debugging on with the "pdb" command inside it.

I then run my script with the "run myscript.py" command in the directory where my script is located.

If I get an exception, ipython stops the program inside the debugger. Check out the help command for the magic ipython commands (%magic)

感情旳空白 2024-08-03 03:52:58

我喜欢简单地将这一行代码粘贴到我想要设置断点的脚本中:

__import__('IPython').Debugger.Pdb(color_scheme='Linux').set_trace()

较新的版本可能会使用:

__import__('IPython').core.debugger.Pdb(color_scheme='Linux').set_trace()

I like to simply paste this one-liner in my scripts where I want to set a breakpoint:

__import__('IPython').Debugger.Pdb(color_scheme='Linux').set_trace()

Newer version might use:

__import__('IPython').core.debugger.Pdb(color_scheme='Linux').set_trace()
从此见与不见 2024-08-03 03:52:58

看起来模块最近被重新调整了一些。 在 IPython 0.13.1 上,以下内容对我有用

from IPython.core.debugger import Tracer; breakpoint = Tracer()

breakpoint() # <= wherever you want to set the breakpoint

尽管可惜,这一切在 qtconsole 中都毫无价值。

Looks like modules have been shuffled around a bit recently. On IPython 0.13.1 the following works for me

from IPython.core.debugger import Tracer; breakpoint = Tracer()

breakpoint() # <= wherever you want to set the breakpoint

Though alas, it's all pretty worthless in qtconsole.

╭⌒浅淡时光〆 2024-08-03 03:52:58

较新版本的 IPython 提供了一种简单的机制,用于将 IPython 会话嵌入和嵌套到任何 Python 程序中。 您可以按照以下方法嵌入 IPython 会话:

try:
    get_ipython
except NameError:
    banner=exit_msg=''
else:
    banner = '*** Nested interpreter ***'
    exit_msg = '*** Back in main IPython ***'

# First import the embed function
from IPython.frontend.terminal.embed import InteractiveShellEmbed
# Now create the IPython shell instance. Put ipshell() anywhere in your code
# where you want it to open.
ipshell = InteractiveShellEmbed(banner1=banner, exit_msg=exit_msg)

然后,每当您想要进入 IPython shell 时,都可以使用 ipshell() 。 这将允许您在代码中嵌入(甚至嵌套)IPython 解释器。

Newer versions of IPython provide an easy mechanism for embedding and nesting IPython sessions into any Python programs. You can follow the following recipe to embed IPython sessions:

try:
    get_ipython
except NameError:
    banner=exit_msg=''
else:
    banner = '*** Nested interpreter ***'
    exit_msg = '*** Back in main IPython ***'

# First import the embed function
from IPython.frontend.terminal.embed import InteractiveShellEmbed
# Now create the IPython shell instance. Put ipshell() anywhere in your code
# where you want it to open.
ipshell = InteractiveShellEmbed(banner1=banner, exit_msg=exit_msg)

Then use ipshell() whenever you want to drop into an IPython shell. This will allow you to embed (and even nest) IPython interpreters in your code.

入怼 2024-08-03 03:52:58

来自 IPython 文档

import IPython.ipapi
namespace = dict(
    kissa = 15,
    koira = 16)
IPython.ipapi.launch_new_instance(namespace)

将以编程方式启动 IPython shell。 显然,namespace 字典中的值只是虚拟值 - 在实践中使用 locals() 可能更有意义。

请注意,您必须将其硬编码到; 它不会像 pdb 那样工作。 如果这就是您想要的,DoxaLogos 的答案可能更像您正在寻找的。

From the IPython docs:

import IPython.ipapi
namespace = dict(
    kissa = 15,
    koira = 16)
IPython.ipapi.launch_new_instance(namespace)

will launch an IPython shell programmatically. Obviously the values in the namespace dict are just dummy values - it might make more sense to use locals() in practice.

Note that you have to hard-code this in; it's not going to work the way pdb does. If that's what you want, DoxaLogos' answer is probably more like what you're looking for.

红颜悴 2024-08-03 03:52:58

快速简单的方法:

from IPython.Debugger import Tracer
debug = Tracer()

编写即可。

debug()

然后只需在您想要开始调试程序的任何地方

The fast-and-easy way:

from IPython.Debugger import Tracer
debug = Tracer()

Then just write

debug()

wherever you want to start debugging your program.

飘落散花 2024-08-03 03:52:58

在过去的几天里,我不得不在谷歌上搜索几次,所以添加了一个答案...有时,能够正常运行脚本并且仅在出现错误时才进入 ipython/ipdb ,而不必将 ipdb.set_trace 放入 ipython/ipdb 中,这很好。 () 断点进入代码

ipython --pdb -c "%run path/to/my/script.py --with-args here"

(当然首先是 pip install ipythonpip install ipdb

I had to google this a couple if times the last few days so adding an answer... sometimes it's nice to be able run a script normally and only drop into ipython/ipdb on errors, without having to put ipdb.set_trace() breakpoints into the code

ipython --pdb -c "%run path/to/my/script.py --with-args here"

(first pip install ipython and pip install ipdb of course)

节枝 2024-08-03 03:52:58

这非常简单:

ipython some_script.py --pdb

它需要安装 iPython,通常这可以工作:
pip install ipython

我使用 ipython3 而不是 ipython,所以我知道它是 Python 的最新版本。

这很容易记住,因为您只需使用 iPython 而不是 python,并将 --pdb 添加到末尾。

This is pretty simple:

ipython some_script.py --pdb

It needs iPython installing, usually this works:
pip install ipython

I use ipython3 instead of ipython, so I know it's a recent version of Python.

This is simple to remember because you just use iPython instead of python, and add --pdb to the end.

只涨不跌 2024-08-03 03:52:58

要使用终端颜色获取 IPython 的 REPL,我必须这样做:

from IPython import start_ipython

start_ipython()

https://ipython.readthedocs.io/en/stable/api/ generated/IPython.html#IPython.start_ipython

To get IPython's REPL using terminal colors, I had to do this:

from IPython import start_ipython

start_ipython()

https://ipython.readthedocs.io/en/stable/api/generated/IPython.html#IPython.start_ipython

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文