Skip to content

input() doesn't print the prompt if stderr is redirected #92715

Description

@DavidNemeskey

Bug 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

Activity

  1. changed the title [-]input() doesn't print anything if stderr is redirected[/-] [+]input() doesn't print the prompt if stderr is redirected[/+] on May 12, 2022
  2. sweeneyde commented on May 12, 2022

    @sweeneyde
    Member
  3. DavidNemeskey commented on May 12, 2022

    @DavidNemeskey
    Author

    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".

  4. headst1337 commented on Oct 15, 2024

    @headst1337

    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 >
  5. DavidNemeskey commented on Oct 16, 2024

    @DavidNemeskey
    Author

    @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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    type-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions