python-3.xshellflagspython-interactive

How to hide >>> prompt in python3 interactive console? Is there a flag for this?


For copy and paste purposes, I wish to hide the >>> and ... Python prompts whenever I am in the interactive shell console. I have achieved this effect successfully with the command import sys, then sys.ps1 = "" (source). However that only hides >>>. Furthermore, a flag would simplify the work, and since other languages have this type of flag, I wonder if I'm not reinventing the wheel.

In database programming with Db2, for example, there is a +p flag. As it is described negatively, it does the opposite of -p:

The -p option tells the command line processor to display the command line processor prompt when the user is in interactive mode.

Any shortcuts?


Solution

  • python -i scriptname.py runs scriptname.py, then drops you to an interactive shell. If that script clears sys.ps1 and sys.ps2, then the interactive shell will be one that doesn't print prompts.

    Even better, you can use the shell feature process substitution to create a temporary filename (something like /dev/fd/10, depending on your OS) that, when read, contains exactly the script you want:

    pnp() { python -i <(printf '%s\n' 'import sys' 'sys.ps1=""' 'sys.ps2=""') "$@"; }
    

    ...if placed in your .bashrc, will define a command pnp ("python, no prompt") that does the above.