r/learnpython • u/[deleted] • Mar 05 '21
Python input() function writes prompt to standard error instead of standard output
I'm using Python 3.9.2 on Arch Linux.
I'm trying to make an interactive shell program in Python. I ran into an issue where the shell prompt would sometimes come before the output of the previous command. Here's a small example program that demonstrates the issue:
while True:
cmd=input('>>> ')
print('This is the output')Around 80% of the time, the program works as expected:
>>> hello
This is the output
>>> goodbye
This is the output
>>> test
This is the outputHowever, occasionally, this happens:
>>> hello
>>> This is the outputAs you can see, on the second line, the >>> prompt is printed before This is the output is printed.
I tried to use strace to debug the program, and I found out that the input() function actually writes the prompt to standard error, not standard output (when the program is run interactively). Here are the relevant syscalls:
write(2, ">>> ", 4) = 4
read(0, "hello\n", 1024) = 6
write(1, "This is the output\n", 19) = 19
write(2, ">>> ", 4) = 4You can see from the syscalls that the print() function writes to file descriptor 1 (standard output), but the input() function writes to file descriptor 2 (standard error).
This seems to contradict what the documentation says:
If the prompt argument is present, it is written to standard output without a trailing newline. The function then reads a line from input, converts it to a string (stripping a trailing newline), and returns that.
Is there a way to force input() to write the prompt to standard output instead of standard error?
20
u/shiftybyte Mar 05 '21
This is actually really interesting... will this solve your problem?
while True:
print('>>> ')
cmd=input()
print('This is the output')27
Mar 05 '21
Yes, it will solve my problem, but
print()by default appends a newline, so there should be anend=''argument:while True: print('>>> ',end='') cmd=input() print('This is the output')I was looking for a way to print the prompt properly with a single call to
input(), but if that isn't possible, I'm probably going to use this solution.Thanks!
4
u/aroberge Mar 06 '21
How about redefining input? Something (untested) like:
old_input = input def input(prompt): print(prompt, end='') return old_input()
5
u/exhuma exhuma Mar 06 '21
Top notch question! It makes me believe that you have experience but are only now getting into Python. So while I generally avoid answers like "you shouldn't do it this way. Do X instead" as those are really not helpful for a learner at all.
But I wager that you are not a beginner.
With that out of the way, if the script you are writing is not just an exercise for learning Python, but part of an application which you do intend to use in the future, have a look at the stdlib cmd module or even prompt-toolkit which will give you a nicer user-experience without much more effort.
1
255
u/TravisJungroth Mar 05 '21
I just want to say this is the best question on /r/learnpython I've ever seen. Interesting problem, test cases, exploration, documentation, even frickin' version numbers. Top tier.
126
u/tanto_von_scumbag Mar 06 '21
What does this person think this is? StackOverflow?
3
u/Slow-Scallion4183 Mar 06 '21
yeah but fortunately r/learnpyton isnt as harsh as stackoverflow. Thats why I usualy asks my questions here first
57
26
u/Se7enLC Mar 06 '21
And the FORMATTING! We've got code blocks with properly formatted code in them, we've got inline code for functions, quote blocks with italics and bold text.
It's already solved but I want to try to solve it again just to encourage this kind of question!
5
u/a1brit Mar 06 '21
To solve the first issue without getting into the other stderr stuff.
Add flush=True) to your print command.
0
-18
u/Jefaxe Mar 05 '21
Commenting so I get notified
15
u/garlic_bread_thief Mar 06 '21
Do you get notified when there's a new comment in the post? This isn't Facebook
-3
0
127
u/debian_miner Mar 05 '21
Looks like this was reported as a bug: https://bugs.python.org/issue25692.
Edit: You can work around your issue by calling
sys.stdout.flush()before your call toinput()