Python getpass.getpass() function call hangs

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-30 17:17:29

问题


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

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




回答3:


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("")



回答4:


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()
    # ...


来源:https://stackoverflow.com/questions/24544353/python-getpass-getpass-function-call-hangs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!