Open In App

How to get the current username in Python

Last Updated : 15 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

When building interacting Python programs you might need to get the current username for features like personalizing user experience, providing user-specific resources, Implementing robust logging and debugging, strengthening security, etc.

We will use different Python modules and functions like os.getlogin(), os.path.expanduser(), environ.get() method to get the current username in Python.

In this tutorial, we will discuss how to get the current username in Python.

5 Ways of Getting Current Username

Getting the current username in Python is a very simple task. Python has provided multiple built-in libraries and functions to get the current username.

The 5 such methods to get the current username are:

  1. os.getlogin()
  2. os.path.expanduser()
  3. os.environ.get()
  4. getpass.getuser()
  5. pwd.getpwuid() or os.getuid()

Using OS library

OS library in Python allows you to directly interact with the operating system. There are multiple functions you can use from the OS library to get the current username:

  1. os.getlogin()
  2. os.path.expanduser()
  3. os.environ.get()

Using the getlogin() method to get the current username

getlogin() method of OS library is used to get the current username.

Syntax :

 os.getlogin( ) 

Example 1:  Using getlogin() method

Python3
# importing os module
import os


# using getlogin() returning username
os.getlogin()

Output :

'KRISHNA KARTHIKEYA'

Using the expanduser() method to get the current username

os.path.expanduser() method retrieves the name of the user currently logged in to the operating system.

Example: Get username using os.path.expanduser() method 

There is another method available in the OS library named path.expanduser() method. In this function, we need to pass the Tilde operator within single quotes as an argument.

Syntax :

os.path.expanduser( ) 

Note: In this method, we need to pass the tilde operator as an argument.

Python3
# importing required module
import os

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

Output :

'C:\\Users\\KRISHNA KARTHIKEYA'

Using the environ.get() method to get the current username

You can use os.environ.get() method to get the current username from the USERNAME environment variable.

Example 3: Get username using os.environ.get() method

This method is also available in the os module. We need to pass USERNAME as an argument into this method. Let us see the syntax and example.

Syntax :

os.environ.get( " USERNAME" )

Note: In some cases, we need to pass USER instead of USERNAME. In most cases, we pass USERNAME as an argument.

Python3
# importing os module
import os

# using environ.get() method getting
# current username
os.environ.get('USERNAME')

Output :

'KRISHNA KARTHIKEYA'

Using getpass library

getpass library in Python is used to get sensitive operations from the user. We can use getuser() method from getpass library to return the current username. 

Syntax :

 getpass.getuser( )

Example: Get the current username using the getuser() function.

Python3
# importing getpass library using import command
# Here gt is a alias name for getpass
# Instead of writing getpass we can use gt
import getpass as gt

# using getuser() method , returning current 
# username
gt.getuser()

Output :

'KRISHNA KARTHIKEYA'

Using os and pwd modules

pwd module works only with a Linux environment. However, the OS module works with both Windows and Linux. This means some methods work with only Windows and some methods work only with Linux. If we execute this method in Linux we will get output as root.

We can use getpwuid() method from pwd module and getuid() method from the OS module to get the user ID.

Using getpwuid() or os.getuid() to get the user-id

getpwuid() method returns the user information by UID.

getuid() method returns the current process UID.

Syntax :

 getpwuid( os.getuid() )[0]

Note: Here [0] is like an index. Generally, this function returns many outputs like system name, password, uid, bash, etc. Here we need a username. It is at index 0 . so we are specifying [0].

Example: Get username using pwd.getpwuid() and os.getuid() method 

Python3
# importing required modules
import os
import pwd

# Using getpwuid() and getuid we are
# printing current username
pwd.getpwuid(os.getuid())[0]

Output :

'root'

We have mentioned the 5 ways you can use to get the current username. We have discussed each method with respective examples for your better understanding. All these methods let you communicate directly with the operating system to fetch the user name.

Similar Reads:



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads