Python getpass.getpass() function call hangs

后端 未结 5 926
广开言路
广开言路 2021-01-06 01:10

I am trying to get a prompt that will ask for my password but when I try to call getpass.getpass() it just freezes. I am running on Windows 7 64 bit using Pytho

相关标签:
5条回答
  • 2021-01-06 01:35

    Python "effectively freezes because it can't receive the input from standard input". See https://support.enthought.com/entries/22157050-Canopy-Python-prompt-QtConsole-Can-t-run-getpass-or-interactive-OS-shell-commands-or-Windows-process

    The fix is to use a different interpreter. I switched to IDLE and fixed the issue.

    0 讨论(0)
  • 2021-01-06 01:35

    getpass() will freeze if python is unable to read properly from standard input. This can happen on e.g. some Windows terminals, such as using git bash. You can use the sys module to detect if this will happen, to avoid hanging:

    import getpass
    import sys
    
    # ...
    
    if not sys.stdin.isatty():
        # notify user that they have a bad terminal
        # perhaps if os.name == 'nt': , prompt them to use winpty?
        return
    else:
        password = getpass.getpass()
        # ...
    
    0 讨论(0)
  • 2021-01-06 01:42

    I also had this on mac with both Jupyter Lab and Jupyter Notebook. For me the issue was caused by the variable name.

    Naming the variable PG_REMOTEPASSWORD caused a hang but PG_PASSWORD & PG_ABCPass didn't. I don't know why that is an issue, there's nothing in docs about restrictions on what a variable can be called.

    My setup up is Anaconda running Python 3.7.7

    0 讨论(0)
  • 2021-01-06 01:45

    It is correct that Python "effectively freezes because it can't receive the input from standard input", however, for windows you can prefix your command with winpty. Then password can be inputted correctly when started like:

    winpty python fileToExecute.py
    

    winpty provides a interface similar to a Unix pty-master in a way that communication is also possible from windows terminals.

    0 讨论(0)
  • 2021-01-06 01:47

    Faced the same issue with getpass (mingw64) and found this simple solution.

    os.system("stty -echo")
    password = input('Enter Password:')
    os.system("stty echo")
    print("")
    
    0 讨论(0)
提交回复
热议问题