Python getpass Module


There are two functions defined in getpass module of Python’s standard library. They are useful whenever a terminal based application needs to be executed only after validating user credentials.

getpass()

This function prompts the user to enter a password. By default the keys entered by user in the terminal are not echoed. Also the default prompt that appears on terminal is ‘password’ which can be customized by providing a string as parameter.

In following example, Python prompt is invoked from Command prompt terminal on Windows. The password entered is not echoed in the terminal.

C:\python36>python
Python 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 17:00:18) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import getpass
>>> pwd=getpass.getpass("enter pssword:")
enter pssword:
>>> pwd
'admin'

However, if IDLE software is used for interactive session of Python, it doesn’t provide echo free input. Hence password entered is echoed.

Python Code

getuser()

This function returns login name of the user.

>>> getpass.getuser()
'acer'

Updated on: 26-Jun-2020

949 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements