Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/348.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从脚本运行交互式Python控制台时键盘快捷键断开_Python_Shell - Fatal编程技术网

从脚本运行交互式Python控制台时键盘快捷键断开

从脚本运行交互式Python控制台时键盘快捷键断开,python,shell,Python,Shell,可以使用以下代码从脚本内部启动交互式控制台: import code # do something here vars = globals() vars.update(locals()) shell = code.InteractiveConsole(vars) shell.interact() 当我这样运行脚本时: $ python my_script.py 将打开一个交互式控制台: Python 2.7.2+ (default, Jul 20 2012, 22:12:53) [GC

可以使用以下代码从脚本内部启动交互式控制台:

import code

# do something here

vars = globals()
vars.update(locals())
shell = code.InteractiveConsole(vars)
shell.interact()
当我这样运行脚本时:

$ python my_script.py
将打开一个交互式控制台:

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)
>>>
控制台加载了所有全局和局部,这非常好,因为我可以轻松地测试东西

这里的问题是箭头在启动Python控制台时不能正常工作。它们只是向控制台显示转义字符:

>>> ^[[A^[[B^[[C^[[D
这意味着我无法使用上/下箭头键调用以前的命令,也无法使用左/右箭头键编辑行

有人知道这是为什么和/或如何避免吗?

查看并:

这是我使用的一个:

def debug_breakpoint():
    """
    Python debug breakpoint.
    """
    from code import InteractiveConsole
    from inspect import currentframe
    try:
        import readline # noqa
    except ImportError:
        pass

    caller = currentframe().f_back

    env = {}
    env.update(caller.f_globals)
    env.update(caller.f_locals)

    shell = InteractiveConsole(env)
    shell.interact(
        '* Break: {} ::: Line {}\n'
        '* Continue with Ctrl+D...'.format(
            caller.f_code.co_filename, caller.f_lineno
        )
    )

例如,考虑以下脚本:

a = 10
b = 20
c = 'Hello'

debug_breakpoint()

a = 20
b = c
c = a

mylist = [a, b, c]

debug_breakpoint()


def bar():
    a = '1_one'
    b = '2+2'
    debug_breakpoint()

bar()
执行时,此文件显示以下行为:

$ python test_debug.py
* Break: test_debug.py ::: Line 24
* Continue with Ctrl+D...
>>> a
10
>>>
* Break: test_debug.py ::: Line 32
* Continue with Ctrl+D...
>>> b
'Hello'
>>> mylist
[20, 'Hello', 20]
>>> mylist.append(a)
>>>
* Break: test_debug.py ::: Line 38
* Continue with Ctrl+D...
>>> a
'1_one'
>>> mylist
[20, 'Hello', 20, 20]

如果可以的话,我会给你本月最佳答案奖:)简短而准确:)因为Windows上不包括
readline
,所以可以使用
pyreadline
来实现这个解决方案;只需安装
pyreadline
并在导入
readline
之前导入即可。不过,您可能需要使用
vars
以外的另一个名称,以免覆盖内置的
vars
函数。要从代码运行ipython shell,请执行以下操作:
从ipython导入嵌入;embed()
(变量以透明方式传递,包括局部变量)。
$ python test_debug.py
* Break: test_debug.py ::: Line 24
* Continue with Ctrl+D...
>>> a
10
>>>
* Break: test_debug.py ::: Line 32
* Continue with Ctrl+D...
>>> b
'Hello'
>>> mylist
[20, 'Hello', 20]
>>> mylist.append(a)
>>>
* Break: test_debug.py ::: Line 38
* Continue with Ctrl+D...
>>> a
'1_one'
>>> mylist
[20, 'Hello', 20, 20]