58

Can I get console input without echo in Python?

2
  • Console input and echo? Does not compute. Need more detail. Commented Jan 6, 2011 at 15:56
  • 2
    @Daniel I think they're using "echo" to mean "printing program inputs", similar to CMD's @echo on Commented May 31, 2023 at 18:43

3 Answers 3

81

Use getpass:

>>> from getpass import getpass
>>> getpass()
Password:
'secret'
Sign up to request clarification or add additional context in comments.

3 Comments

I would like character by character like getch() but cross platform
@tm1rbrt In that case, curses is probably your best option.
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.
11

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

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

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

It's not that cross-platform at this point --- it only supports Windows through Windows 2000.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.