What is the getpass.getuser() method in Python?

Overview

The getpass module in Python yields a platform-independent approach to entering or adding passwords in a command-line program.

The getuser() method is used to extract the current logged-in username or login name. In order to return, it checks system environment variables LOGNAME, USER, LNAME and USERNAME. If the value of the first one is set, it will return the login name. Otherwise, it will return None or raise an exception.

Syntax


os.getuser()


Parameters

It does not take any argument value.

Return value

It returns logged-in user names from environment variables LOGNAME, USER, LNAME, and USERNAME. If None is set, it returns the login name from the password database.

Explanation

In the code snippet, we are going to include the getpass module and invoke the getuser() method to get the current logged-in username.

# Show working of getpass.getuser() function
# including getpass module
import getpass
# invoking getuser() to extract
# current parent process name
user = getpass.getuser()
# print it on console
print(user)

Free Resources