55

Can I get console input without echo in python?

3 Answers3

76

Use getpass:

>>> from getpass import getpass
>>> getpass()
Password:
'secret'
Josh Lee
  • 171,072
  • 38
  • 269
  • 275
  • 1
    I would like character by character like getch() but cross platform –  Jan 06 '11 at 15:59
  • @tm1rbrt In that case, [curses](http://docs.python.org/library/curses.html) is probably your best option. – Josh Lee Jan 06 '11 at 16:00
  • 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. – Apalala Jan 06 '11 at 16:52
12

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"
Michael
  • 129
  • 1
  • 2
  • 1
    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. – Logan Byers Jun 14 '17 at 20:51
0

Maybe the 'console' module is your only bet (it's kinda '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.

mguillech
  • 336
  • 1
  • 2