0

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?

  • 1
    What's the goal (the reason you're here asking for a different approach)? Is the intent to have this already done before you're in the interpreter? (If so, there are a few ways to specify code to run at interpreter startup, so that same `sys.ps1 = ""` can be invoked without you having to enter it yourself). If you want a solution that doesn't involve changing `sys.ps1` _at all_, that's different. – Charles Duffy Feb 19 '23 at 16:51
  • it looks like you already have an answer that works pretty well, no? – njzk2 Feb 19 '23 at 16:52
  • It gets annoying to write that every time, if there's already a flag for it. I assume I can take the answer @Sanu Uthaiah Bollera provided one step further and create an alias for `python -i my_python_startup.py` if I want a flag-like effect, or I can just change the startup file to keep a more permanent change. I just thought there would already be a flag for it – Lucas de Assis Feb 19 '23 at 17:00
  • Why would you assume a flag in particular? You can see what the flags are with `python --help`; not there == not a flag. But, say, `PYTHONSTARTUP` will work fine. – Charles Duffy Feb 19 '23 at 19:56
  • BTW, stay away from shell aliases; they're considered a misfeature for good reasons. In almost every case, a shell function is more appropriate than an alias. Functions can perform conditional logic; they let you pass arguments through at any position you like, vs just appending remaining command-line content to the end; they work (at all) in scripts when defined there (whereas alias support in scripts needs to be explicitly turned on); they let you use the same escaping syntax you use everywhere else in the shell; etc etc. – Charles Duffy Feb 19 '23 at 19:57
  • Since the answer covering the essentials of the above has been deleted, I'll incorporate that shell function into an answer of my own. And while you might not be able to cast upvotes yet, you should be able to accept answers :) – Charles Duffy Feb 20 '23 at 00:25

1 Answers1

1

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.

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441