Get Current User Name in Python

Get Current User Name in Python

  1. Method 1 - Use the getpass Module to Get Current User Name in Python
  2. Method 2 - Use the os and pwd Modules to Get Current User Name in Python
  3. Method 3 - Use the psutil Module to Get Current User Name in Python
  4. Method 4 - Use the os Module to Get Current User Name in Python

This short article explains the possible ways or libraries to get the current username in Python. Here, username refers to the user of the underlying operating system (e.g., Windows user, Linux user, etc.). For this tutorial, we are using Linux operating system.

Method 1 - Use the getpass Module to Get Current User Name in Python

When we use a terminal-based application that requires security credentials like username and password before executing the application, we may use the getpass module’s built-in functions. For example, for the username, run the below code.

import getpass
getpass.getuser()

We must first install the getpass library to retrieve the user’s name. After that, we can use the getpass.getuser() function. The user’s system login name is returned by this method.

It searches our computer’s environment variables for the user’s name and returns it as a string; an exception is produced if it cannot find the environment variable. The above code produces the following output:

'root'

Method 2 - Use the os and pwd Modules to Get Current User Name in Python

The Python os module is essential and one of the standard utility modules. It provides the functionality to interact with the operating system. This module will enable us to use operating system-dependent functions on the go.

If we do not provide the correct file names, paths, or other related parameters that have the right type but are not identified by the OS, all functions in the os module throw an Operating System Error.

import os
import pwd
def get_username():
    return pwd.getpwuid(os.getuid())[0]

In the above code, os.getuid() is used to get the user’s real ID of the current process. It does not take any argument. It is used as the search criterion for the function getpwuid(). The getpwuid() function should return a reference to the structure holding the user ID value.

get_username()

The above code returns the following output:

'root'

Method 3 - Use the psutil Module to Get Current User Name in Python

Python’s library psutil lets us get detailed information about the system and processes. It is used to track how the system’s various resources are being consumed. For example, it can monitor the utilization of resources like the CPU, RAM, storage, network, and sensors.

import psutil
def get_username():
    return psutil.Process().username()

psutil.Process.username() returns the system’s username from the current process details.

get_username()

The above code gives the following output:

'root'

Method 4 - Use the os Module to Get Current User Name in Python

As already described, the os module is used to interact with the operating system, and this module allows us to use operating system-dependent functions on the go.

In Python, the os.path.expanduser() function is used to expand a path component or user in the specified path to the user’s home directory.

An initial (~) on Unix platforms is substituted by the value of the HOME environment variable if it is set. Otherwise, the os.path.expanduser() function searches the password directory for the user’s home directory using the built-in module pwd.

A path with an initial user item can be directly searched-up in the password directory.

import os
# using path.expanduser() getting username
os.path.expanduser('~')

The above code gives the following output:

'/root'