How to get the current username in Python
Last Updated :
15 Mar, 2024
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:
- os.getlogin()
- os.path.expanduser()
- os.environ.get()
- getpass.getuser()
- 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:
- os.getlogin()
- os.path.expanduser()
- 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
[GFGTABS]
Python3
# importing os module
import os
# using getlogin() returning username
os.getlogin()
[/GFGTABS]
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.
[GFGTABS]
Python3
# importing required module
import os
# using path.expanduser() getting username
os.path.expanduser('~')
[/GFGTABS]
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.
[GFGTABS]
Python3
# importing os module
import os
# using environ.get() method getting
# current username
os.environ.get('USERNAME')
[/GFGTABS]
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.
[GFGTABS]
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()
[/GFGTABS]
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
[GFGTABS]
Python3
# importing required modules
import os
import pwd
# Using getpwuid() and getuid we are
# printing current username
pwd.getpwuid(os.getuid())[0]
[/GFGTABS]
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:
Similar Reads
How to get current_url using Selenium in Python?
While doing work with selenium many URL get opened and redirected in order to keeping track of URL current_url method is used. The current_url method is used to retrieve the URL of the webpage the user is currently accessing. It gives the URL of the current webpage loaded by the driver in selenium.
2 min read
How to Get the Currently Logged In User's ID in Django?
Getting the ID of the currently logged-in user is a very common requirement when building user-centered web applications in Django. Through its built-in authentication system, Django allows easy access to the details of the user who is logged in at any time; this includes their unique ID. We can eas
4 min read
How to use String Formatters in Python
In Python, we use string formatting to control how text is displayed. It allows us to insert values into strings and organize the output in a clear and readable way. In this article, weâll explore different methods of formatting strings in Python to make our code more structured and user-friendly. U
3 min read
How to get value from address in Python ?
In this article, we will discuss how to get the value from the address in Python. First, we have to calculate the memory address of the variable or python object which can be done by using the id() function. Syntax: id(python_object) where, python_object is any python variable or data structure like
4 min read
How to Store Username and Password in Flask
This article covers storing usernames and passwords in a Flask web app using MySQL. After logging in, users see a welcome message with their username. InstallationTo make our project we first create a virtual environment, to learn how to create and activate a virtual environment, refer to - Python v
6 min read
Python | How to get function name ?
One of the most prominent styles of coding is following the OOP paradigm. For this, nowadays, stress has been to write code with modularity, increase debugging, and create a more robust, reusable code. This all encouraged the use of different functions for different tasks, and hence we are bound to
3 min read
Python - API.get_user() in Tweepy
Twitter is a popular social network where users share messages called tweets. Twitter allows us to mine the data of any user using Twitter API or Tweepy. The data will be tweets extracted from the user. The first thing to do is get the consumer key, consumer secret, access key and access secret from
3 min read
Python Tweepy â Getting the time when a user was created
In this article we will see how we can get the time and date when a user created their Twitter account. The created_at attribute provides us with a datetime object denoting the exact time when the user account was created. Identifying the date when a user was created in the GUI : In the above mentio
2 min read
Python PRAW â Getting the username of a redditor
In Reddit, a redditor is the term given to a user. Each and every redditor has a username chosen by them during registration. Here we will see how to fetch the username of a redditor. We will be using the name attribute of the Redditor class to fetch the username. Example 1 : Consider the following
2 min read
How to generate a unique username using Python
A username is a unique name that is mainly used on websites and apps. A username is used to identify a person. For example, if your name is Akshay and you want to create your account on any social media app, here we need a username such that another person can find us. It is not possible to search b
4 min read