Python getpass Module

Getpass Module

Python getpass module enables to prompt the user for a password without displaying it on the screen. Thus, it can customize the user’s experience. Python’s getpass module enables secure and user-friendly handling of password input. It lets you prompt the user for a password in a way that doesn’t display their input on the screen, enhancing the security of your Python applications

The Python getpass module provides a secure way to prompt for a user’s password without displaying it. Its primary function, getpass.getpass(), enables customization of user experience, and getpass.getuser() function retrieves the corresponding login name of the user/system

Python getpass Module Examples

Import:

import getpass

Syntax:

getpass.getpass([prompt[, stream]])

Simple Example:

Here we use the basic getpass function. We wrap the function in a try-except block for error handling. If the user successfully enters a password, it is stored in the pass_word variable and then printed. If there’s an error, it’s captured and printed instead

import getpass

try:
    pass_word = getpass.getpass()
except Exception as E:
    print('There is an Error : ', E)
else:
    print('Password fetched from command prompt :', pass_word)

Output:

Password: Safa
Password fetched from command prompt : Safa

1. getpass module with no prompt

Let’s see the use of getpass without a custom prompt. The function will use a default ‘Password:’ prompt

Getpass Module
Exploring the Getpass Module in Python

2. Customizing Prompts with Getpass

You can customize the getpass prompt to enhance user interaction. Here, we change the prompt to ask the user about their favorite place, providing a more engaging experience

import getpass

place = getpass.getpass(prompt = 'Which is your favorite place to go?')
if place == 'Satara':
    print('Ofcourse!')
else:
    print('Where is that?')

Output:

Which is your favorite place to go?
Ofcourse!
Getpass Module With Custom Prompt
Customizing Prompts with Getpass

3. Directing Getpass Output to Other Streams

getpass also allows you to redirect the password prompt to different output streams. In this example, we use sys.stderr as the output stream, meaning the password prompt will be directed to the standard error stream.

import getpass
import sys

pass_word = getpass.getpass(stream=sys.stderr)
print('Entered password:', pass_word)

Output:

Password:
Entered password: Safa
Getpass Module With Other Stream
Directing Getpass Output to Other Streams

4. getpass(argument) Function in Python

The getpass(argument) function enables us to print the prompt string on the command prompt. The getpass function can accept an argument that serves as the custom prompt. Here, we use getpass.getuser() to get the current system user and then prompt the user for a password with a custom prompt that includes the user’s name.

If the argument is omitted, it prints ‘Password:’.

import getpass

user = getpass.getuser()

pass_word = getpass.getpass("User's password %s: " % user)

print(user, pass_word)

Output:

User's password HP:
HP safa

5. getuser() Function in Getpass Module

The getuser() function is another utility from the getpass module. It fetches the login name of the user from the environment variables, providing a portable way to get user information.

getpass.getuser()

Output:

HP

Summary

With Python’s getpass module, you can securely prompt users for passwords and enhance user experience. This versatile tool empowers you to customize prompts, manage output streams, and even fetch system login names. What’s your next security challenge that getpass could help solve?


References