您可以使用以下代码从脚本内部启动交互式控制台:
import code
# do something here
vars = globals()
vars.update(locals())
shell = code.InteractiveConsole(vars)
shell.interact()
Run Code Online (Sandbox Code Playgroud)
当我像这样运行脚本时:
$ python my_script.py
Run Code Online (Sandbox Code Playgroud)
交互式控制台打开:
Python 2.7.2+ (default, Jul 20 2012, 22:12:53)
[GCC 4.6.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>>
Run Code Online (Sandbox Code Playgroud)
控制台有所有全局和本地加载,这很好,因为我可以轻松测试东西.
这里的问题是箭头在启动Python控制台时不像通常那样工作.他们只是将转义字符显示到控制台:
>>> ^[[A^[[B^[[C^[[D
Run Code Online (Sandbox Code Playgroud)
这意味着我无法使用向上/向下箭头键调用以前的命令,也无法用左/右箭头键编辑行.
有谁知道为什么会这样和/或如何避免这种情况?
Chr*_*our 33
import code
import readline
import rlcompleter
# do something here
vars = globals()
vars.update(locals())
readline.set_completer(rlcompleter.Completer(vars).complete)
readline.parse_and_bind("tab: complete")
shell = code.InteractiveConsole(vars)
shell.interact()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4762 次 |
| 最近记录: |