Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python getpass password input with asterisks

You know how in Linux when you try some Sudo stuff it tells you to enter the password and, as you type, nothing is shown in the terminal window (the password is not shown)?

Is there a way to do that in Python? I'm working on a script that requires so sensitive info and would like for it to be hidden when I'm typing it.

In other words, I want to get the password from the user without showing the password.

like image 721
Nacht Avatar asked Nov 18 '22 20:11

Nacht


2 Answers

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.

like image 97
Sven Marnach Avatar answered Dec 21 '22 08:12

Sven Marnach


import getpass

pswd = getpass.getpass('Password:')

getpass works on Linux, Windows, and Mac.

like image 45
Nafscript Avatar answered Dec 21 '22 09:12

Nafscript