Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keyboard shortcuts broken running interactive Python Console from a script

Tags:

python

shell

You can start an interactive console from inside a script with following code:

import code

# do something here

vars = globals()
vars.update(locals())
shell = code.InteractiveConsole(vars)
shell.interact()

When I run the script like so:

$ python my_script.py

an interactive console opens:

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)
>>>

The console has all globals and locals loaded which is great since I can test stuff easily.

The problem here is that arrows don't work as they normally do when starting an Python console. They simply display escaped characters to the console:

>>> ^[[A^[[B^[[C^[[D

This means that I can't recall previous commands using the up/down arrow keys and I can't edit the lines with the left/right arrow keys either.

Does anyone know why is that and/or how to avoid that?

like image 295
ducin Avatar asked Nov 03 '13 15:11

ducin


People also ask

How do I open the console in Python?

The console appears as a tool window every time you choose the corresponding command on the Tools menu. You can assign a shortcut to open Python console: press Ctrl+Alt+S , navigate to Keymap, specify a shortcut for Main menu | Tools | Python or Debug Console.

How do I make Python interactive?

On Windows, bring up the command prompt and type "py", or start an interactive Python session by selecting "Python (command line)", "IDLE", or similar program from the task bar / app menu. IDLE is a GUI which includes both an interactive mode and options to edit and run files.

What is Python terminal?

The Python interactive console (also called the Python interpreter or Python shell) provides programmers with a quick way to execute commands and try out or test code without creating a file.


2 Answers

Check out readline and rlcompleter:

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()
like image 115
Chris Seymour Avatar answered Nov 15 '22 15:11

Chris Seymour


This is the one I use:

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
        )
    )

For example, consider the following script:

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()

When executed, this file shows to following behavior:

$ 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]
like image 28
Havok Avatar answered Nov 15 '22 16:11

Havok