This tutorial is about the getpass library (module) in Python.
The getpass module comprises of only two functions, getpass()
and getuser()
. Lets take a look at how we can utilize these two.
getuser() Function
The getuser() function returns the login name of the user that he is using on his device. This function checks the environment variables LOGNAME
, USER
, LNAME
and USERNAME
, in order, and returns the value of the first one which is set to a non-empty string.
user_name = getpass.getuser()
print(user_name)
CodersLegacy
getpass() Function
The getpass() function is meant to retrieve a password from the user without echoing it back to the input area. This means that while the user is typing in the password, nothing will show onscreen, thus improving the password security.
password = getpass.getpass()
The password will be submitted and saved inside our variable once the user presses enter.
Note: There may be certain conditions in which this does not work, such as the Python terminal.
This marks the end of the Python getpass library. Any suggestions or contributions for CodersLegacy are more than welcome. Relevant questions can be asked in the comments section below.