python:从程序运行交互式python shell

时间:2022-02-19 20:45:12

I often have the case that I'll be writing a script, and I'm up to a part of the script where I want to play around with some of the variables interactively. Getting to that part requires running a large part of the script I've already written.

我经常会遇到这样一个问题,即我将编写一个脚本,并且我要编写脚本的一部分,我希望以交互方式使用一些变量。到达那个部分需要运行我已编写的大部分脚本。

In this case it isn't trivial to run this program from inside the shell. I would have to recreate the conditions of that function somehow.

在这种情况下,从shell内部运行此程序并非易事。我不得不以某种方式重新创建该函数的条件。

What I want to do is call a function, like runshell(), which will run the python shell at that point in the program, keeping all variables in scope, allowing me to poke around in it.

我想要做的是调用一个函数,比如runhell(),它将在程序中的那一点运行python shell,将所有变量保持在范围内,允许我在其中查找。

How would I go about doing that?

我该怎么做呢?

5 个解决方案

#1


43  

import code

code.interact(local=locals())

But using the Python debugger is probably more what you want:

但是使用Python调试器可能更符合您的要求:

import pdb

pdb.set_trace()

#2


13  

By far the most convenient method that I have found is:

到目前为止,我发现最方便的方法是:

import IPython
IPython.embed()

You get all your global and local variables and all the creature comforts of IPython: tab completion, auto indenting, etc.

您将获得所有全局和局部变量以及IPython的所有生物舒适:选项卡完成,自动缩进等。

You have to install the IPython module to use it of course:

您必须安装IPython模块才能使用它:

pip install ipython

#3


5  

You can use the python debugger (pdb) set_trace function.

您可以使用python调试器(pdb)set_trace函数。

For example, if you invoke a script like this:

例如,如果您调用这样的脚本:

def whatever():
    x = 3
    import pdb
    pdb.set_trace()

if __name__ == '__main__':
    whatever()

You get the scope at the point when set_trace is called:

您在调用set_trace时得到范围:

$ python ~/test/test.py
--Return--
> /home/jterrace/test/test.py(52)whatever()->None
-> pdb.set_trace()
(Pdb) x
3
(Pdb) 

#4


4  

For practicality I'd like to add that you can put the debugger trace in a one liner:

为了实用,我想补充一点,您可以将调试器跟踪放在一个单行中:

import pdb; pdb.set_trace()

Which is a nice line to add to an editor that supports snippets, like TextMate or Vim+SnipMate. I have it set up to expand "break" into the above one liner.

这是一个很好的行添加到支持片段的编辑器,如TextMate或Vim + SnipMate。我把它设置为将“break”扩展到上面的一个衬里。

#5


0  

Not exactly a perfect source but I've written a few manhole's before, here is one I wrote for an abandoned pet project http://code.google.com/p/devdave/source/browse/pymethius/trunk/webmud/handlers/konsole.py

不完全是一个完美的来源,但我之前写了一些沙井,这是我为一个废弃的宠物项目写的http://code.google.com/p/devdave/source/browse/pymethius/trunk/webmud/handlers /konsole.py

And here is one from the Twisted Library http://twistedmatrix.com/trac/browser/tags/releases/twisted-8.1.0/twisted/manhole/telnet.py the console logic is in Shell.doCommand

这里有来自Twisted Library的http://twistedmatrix.com/trac/browser/tags/releases/twisted-8.1.0/twisted/manhole/telnet.py,控制台逻辑在Shell.doCommand中

#1


43  

import code

code.interact(local=locals())

But using the Python debugger is probably more what you want:

但是使用Python调试器可能更符合您的要求:

import pdb

pdb.set_trace()

#2


13  

By far the most convenient method that I have found is:

到目前为止,我发现最方便的方法是:

import IPython
IPython.embed()

You get all your global and local variables and all the creature comforts of IPython: tab completion, auto indenting, etc.

您将获得所有全局和局部变量以及IPython的所有生物舒适:选项卡完成,自动缩进等。

You have to install the IPython module to use it of course:

您必须安装IPython模块才能使用它:

pip install ipython

#3


5  

You can use the python debugger (pdb) set_trace function.

您可以使用python调试器(pdb)set_trace函数。

For example, if you invoke a script like this:

例如,如果您调用这样的脚本:

def whatever():
    x = 3
    import pdb
    pdb.set_trace()

if __name__ == '__main__':
    whatever()

You get the scope at the point when set_trace is called:

您在调用set_trace时得到范围:

$ python ~/test/test.py
--Return--
> /home/jterrace/test/test.py(52)whatever()->None
-> pdb.set_trace()
(Pdb) x
3
(Pdb) 

#4


4  

For practicality I'd like to add that you can put the debugger trace in a one liner:

为了实用,我想补充一点,您可以将调试器跟踪放在一个单行中:

import pdb; pdb.set_trace()

Which is a nice line to add to an editor that supports snippets, like TextMate or Vim+SnipMate. I have it set up to expand "break" into the above one liner.

这是一个很好的行添加到支持片段的编辑器,如TextMate或Vim + SnipMate。我把它设置为将“break”扩展到上面的一个衬里。

#5


0  

Not exactly a perfect source but I've written a few manhole's before, here is one I wrote for an abandoned pet project http://code.google.com/p/devdave/source/browse/pymethius/trunk/webmud/handlers/konsole.py

不完全是一个完美的来源,但我之前写了一些沙井,这是我为一个废弃的宠物项目写的http://code.google.com/p/devdave/source/browse/pymethius/trunk/webmud/handlers /konsole.py

And here is one from the Twisted Library http://twistedmatrix.com/trac/browser/tags/releases/twisted-8.1.0/twisted/manhole/telnet.py the console logic is in Shell.doCommand

这里有来自Twisted Library的http://twistedmatrix.com/trac/browser/tags/releases/twisted-8.1.0/twisted/manhole/telnet.py,控制台逻辑在Shell.doCommand中