In this section, we are going learn about the getpass module to handle passwords at runtime. The getpass() module in Python prompts the user to enter a password without echoing. The getpass module is used to handle the password prompt whenever programs interact with a user through the Terminal.
We are going to see some examples of how to use the getpass module:
- Create a script called no_prompt.py and write the following code in it:
import getpass
try:
p = getpass.getpass()
except Exception as error:
print('ERROR', error)
else:
print('Password entered:', p)
In this script, a prompt is not provided for the user. So, by default, it is set to the Password prompt.
Run the script as follows:
$ python3 no_prompt.py
Output :
Password:
Password entered: abcd
- We will provide a prompt...