是否可以在代码中放入一些东西来启动交互式解释器(在Python中)

问题描述 投票:0回答:3

这个想法是能够正常运行代码,除了当它到达特定方法调用时它将在给定范围内启动交互式解释器。有点像停在断点处运行代码。

理想情况下,如果您已经在像 ipython 这样的解释器中,它将返回到该解释器,除了访问解释器范围之外的当前范围。

python debugging interactive
3个回答
5
投票

查看代码模块。

这是一个例子:

import code
a = 1
b = 2
code.interact(local=locals())

输出:

Python 2.7 (r27:82500, Nov 10 2010, 22:46:43) 
[GCC 4.2.1 (Apple Inc. build 5664)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> a
1
>>> b
2
>>>

2
投票

你想要

code


1
投票

您想要启动一个 调试器,而不是从运行 python(没有脚本文件)或 ipython 获得的 REPL?

def example(a, b, c):
  a.apple(b.blah() + c)

  import pdb
  pdb.set_trace()

  c.continuing_on()
  while inspecting(this.code()) in the_debugger:
    print "hooray"

您可以从 pdb 执行任意代码,但它也有方便的命令来检查,同时继续运行现有代码。

© www.soinside.com 2019 - 2024. All rights reserved.