GeezerVison is showing github.com — input() doesn't print the prompt if stderr is redirected · Issue #92715 · python/cpython
input() doesn't print the prompt if stderr is redirected #92715DescriptionBug report
If stderr is redirected, input() doesn't print the prompt. The same happens when stdout is redirected, but that is to be expected.
Code (in test.py):
x = input('prompt: ')
print(x)
When run:
> python test.py
prompt: text
text
> python test.py >out
text
> python test.py 2>err
text
text
The contents of out and err, respectively:
# out
prompt: text
# err
prompt:
The first and second cases are as expected. In the last case, the prompt is not printed, even though it should go into stdout, according to the documentation. Instead, it is printed to err.
Your environment
Activitychanged the title [-]input() doesn't print anything if stderr is redirected[/-] [+]input() doesn't print the prompt if stderr is redirected[/+] on May 12, 2022sweeneyde commented on May 12, 2022 This is probably a duplicate of:
Also related:
DavidNemeskey commented on May 12, 2022 I checked the first issue superficially, and I am not really sure it is a duplicate. Eryk's answer in the thread referenced in the penultimate comment, on the other hand, might explain what I have experienced (out is not a TTY, while the terminal is).
So in a sense this might be duplicate (of the thread at least), but the second last comment on that is from 6 years ago, so I don't know if that issue is considered being "alive". headst1337 commented on Oct 15, 2024 To fix this you can import readline
For example:
ipanov@archlinux ~/u/sandbox > cat test_stderr.py
input_msg = input('Enter text: ')
print(input_msg)
ipanov@archlinux ~/u/sandbox > python test_stderr.py 2>> out.txt
123
123
ipanov@archlinux ~/u/sandbox >
and with fix:
ipanov@archlinux ~/u/sandbox > cat test_stderr.py
import readline
input_msg = input('Enter text: ')
print(input_msg)
ipanov@archlinux ~/u/sandbox > python test_stderr.py 2>> out.txt
Enter text: 123
123
ipanov@archlinux ~/u/sandbox >DavidNemeskey commented on Oct 16, 2024 @headst1337 Thank you; it's good to know that readline fixes the problem. However, I think the default behavior is wrong so this is more like a workaround. Also, I couldn't find any mention of this effect in the documentation either, so that should be updated as well, depending on how this ticket is resolved.
MetadataMetadataDevelopmentNo branches or pull requestsIssue actions
|