Can I get console input without echo in Python?
3 Answers
Use getpass:
>>> from getpass import getpass
>>> getpass()
Password:
'secret'
3 Comments
Apalala
I haven't tried it, but you could also import the
readline module. The docs of getpass don't mention readline, but readline changes the behavior of raw_input(), for instance.There is also another solution (at least on unix systems, I don't know if this is working on Windows). Simply switch off the console output and use raw_input:
os.system("stty -echo")
password = raw_input('Enter Password:')
os.system("stty echo")
print "\n"
1 Comment
Logan Byers
Be warned that
stty -echo will persist until stty echo is called. This includes persisting outside the python session, should raw_input cause python to exit.Maybe the 'console' module is your only bet (it's kind of a 'fork' of the curses module for Unix). However, I haven't seen anything related to terminal echo disabling in its homepage; you might try to dig into it by yourself.
1 Comment
JasonFruit
It's not that cross-platform at this point --- it only supports Windows through Windows 2000.
@echo on