-->

Python getpass.getpass() function call hangs

2019-02-19 21:19发布

问题:

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 Python 2.7 on Canopy.

import sys
import getpass

p = getpass.getpass()
print p

回答1:

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.



回答2:

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

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