r/learnpython • • 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 output

However, occasionally, this happens:

>>> hello
>>> This is the output

As 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)                     = 4

You 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?

362 Upvotes

23 comments sorted by

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

47

u/[deleted] Mar 05 '21

Thanks for finding the bug report!

I was able to get input() to output the prompt to standard output by adding import readline to the top of the file. This also has the benefit of allowing me to use GNU Readline for user input.

33

u/dupelize Mar 06 '21

So input outputting to standard error was an error and it is standard for input to output to standard output?

15

u/MaraudingAvenger Mar 06 '21

Indubitably.

18

u/Caligatio Caligatio Mar 06 '21

If you don't like calling .flush(), you can also set the PYTHONUNBUFFERED=1 environment variable which causes a flush after every print.

This is super recommended for Docker images.

2

u/Sigg3net Mar 06 '21

Cool trick!

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

u/[deleted] Mar 05 '21

Yes, it will solve my problem, but print() by default appends a newline, so there should be an end='' 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

u/[deleted] Mar 08 '21

Thanks for the links, I'll look into them!

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

u/Pastoolio91 Mar 06 '21

That’s an Arch user for ya!

41

u/Apocalypseos Mar 06 '21

Yes, Arch users are known for asking question every half hour

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

u/Below_the_Sea Mar 06 '21

Import time

time.sleep(1)

Could maybe help

-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

u/Jefaxe Mar 06 '21

Apparently not.

0

u/[deleted] Mar 06 '21

[deleted]

1

u/[deleted] Mar 06 '21

How would that solve this issue?!