Forcing prompt to be on newline when embedding Python with

eryk sun eryksun at
Thu Jan 5 07:00:30 EST 2017

On Thu, Jan 5, 2017 at 7:09 AM, H Krishnan <hetchkay at> wrote:
> I am working on embedding Python in my application.

You forgot to tell us the version of Python that you're embedding.

> I have redirected sys.stdin and sys.stdout to call methods from a Qt TextEdit
> widget. Everything works fine except that the Python prompt does not always
> come in a new line:
>>>> dir()
> ['__builtins__', '__doc__', '__name__', '__package__']>>>
> Why doesn't the prompt appear in a new line as with the default stdout?

Are you using code.InteractiveConsole / code.interact?

If not, in what mode do you compile, Py_file_input ("exec") or Py_single_input 
("single")? The latter executes PRINT_EXPR:

    >>> dis.dis(compile('1', '', 'single'))
      1           0 LOAD_CONST               0 (1)
                  3 PRINT_EXPR
                  4 LOAD_CONST               1 (None)
                  7 RETURN_VALUE

PRINT_EXPR in turn calls sys.displayhook on the value it pops from the
stack. The default hook writes the repr of the value and a newline to 
sys.stdout, and it also references the value as "_" in the builtins module (2.x 

More information about the Python-list mailing list