Getting command-line password input in Python

Use getpass.getpass():

from getpass import getpass
password = getpass()

An optional prompt can be passed as parameter; the default is "Password: ".

Note that this function requires a proper terminal, so it can turn off echoing of typed characters – see “GetPassWarning: Can not control echo on the terminal” when running from IDLE for further details.


import getpass

pswd = getpass.getpass('Password:')

getpass works on Linux, Windows, and Mac.


Use getpass for this purpose.

getpass.getpass - Prompt the user for a password without echoing