What is the psutil.users() method in Python?
The psutil module
In Python, psutil (Python system and process utilities) is a package that retrieves information on ongoing processes and #key# system usage (CPU, memory, storage, network, and sensors). We mainly use it for system monitoring, profiling, restricting process resources, and process management.
The module can be installed via pip, as follows:
pip install psutil
users method
The users method returns a list of named tuples with the following fields for users presently connected to the system:
name: This is the name of the user.terminal: This is the pseudo-terminal associated with the user.host: This is the hostname associated with the user.started: This is the time since the epoch as a floating-point number given in seconds.pid: This is the login process PID.
Syntax
psutil.users()
The method takes no parameters.
Example
import psutilprint("The current connected users in the system are: ")print(psutil.users())
Sample code output
Expected output
Explanation
- Line 1: We import the
psutilmodule. - Lines 3–4: We retrieve the current users connected in the system and print them to the console.