代码之家  ›  专栏  ›  技术社区  ›  Geo

有没有可能从代码进入ipython?

  •  77
  • Geo  · 技术社区  · 16 年前

    为了我的调试需要, pdb 相当不错。然而,它将是 许多的 如果我能进去的话,就更酷了 ipython . 这件事有可能吗?

    12 回复  |  直到 7 年前
        1
  •  108
  •   memoselyk    8 年前

    有一个 ipdb 将IPython嵌入到标准PDB中的项目,因此您只需执行以下操作:

    import ipdb; ipdb.set_trace()
    

    它可以通过普通的 pip install ipdb .

    数据库 非常短,因此您也可以创建一个文件,而不是简单的安装 ipdb.py 在python路径的某个位置,并将以下内容粘贴到文件中:

    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)
    
        2
  •  50
  •   Alex Gaudio    14 年前

    在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
    
        3
  •  11
  •   ars    16 年前

    相当于

    import pdb; pdb.set_trace()
    

    对于伊普西顿来说:

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

    这有点冗长,但很高兴知道您是否安装了IPDB。这个 make_session 设置配色方案等需要调用一次,并且 set_trace 可以在任何需要中断的地方拨打电话。

        4
  •  11
  •   chrisdev    13 年前

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

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

    但最好是使用IPDB

        5
  •  8
  •   Jay Atkinson    16 年前

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

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

    如果我得到一个异常,IPython会停止调试器中的程序。查看magic ipython命令的帮助命令(%magic)

        6
  •  8
  •   xApple    13 年前

    我喜欢简单地将这一行粘贴到我的脚本中,在其中设置断点:

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

    较新版本可能使用:

    __import__('IPython').core.debugger.Pdb(color_scheme='Linux').set_trace()
    
        7
  •  6
  •   beardc    12 年前

    看起来模块最近有点混乱。在ipython 0.13.1上,以下内容适用于我

    from IPython.core.debugger import Tracer; breakpoint = Tracer()
    
    breakpoint() # <= wherever you want to set the breakpoint
    

    虽然唉,这一切都很美 worthless in qtconsole .

        8
  •  5
  •   Amelio Vazquez-Reina    12 年前

    新版本的ipython提供了一种简单的机制,可以将ipython会话嵌入和嵌套到任何Python程序中。你可以跟随 the following recipe 要嵌入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)
    

    然后使用 ipshell() 每当你想掉进一个IPython外壳。这将允许您在代码中嵌入(甚至嵌套)IPython解释程序。

        9
  •  3
  •   David Z    16 年前

    IPython docs :

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

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

    请注意,您必须硬编码;这样做行不通。 pdb 做。如果这就是你想要的,多萨罗格斯的答案可能更像你想要的。

        10
  •  3
  •   Viktiglemma    15 年前

    快速简便的方法:

    from IPython.Debugger import Tracer
    debug = Tracer()
    

    然后就写

    debug()
    

    无论您想在哪里开始调试程序。

        11
  •  2
  •   Anentropic    10 年前

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

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

    (首先) pip install ipython pip install ipdb 当然)

        12
  •  0
  •   Luke Stanley    7 年前

    这很简单:

    ipython some_script.py --pdb

    它需要ipython安装,通常可以: pip install ipython

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

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

    推荐文章