Mastering psutil: Install, Explore System Processes & CPU Usage in Python
This article introduces the powerful cross‑platform psutil module, explains how to install it, demonstrates basic commands to list processes, retrieve CPU usage, and explore various useful methods, all illustrated with clear code snippets and screenshots.
psutil is a powerful cross‑platform Python module for retrieving information about running processes and CPU usage, useful for system monitoring and analysis.
Installation pip install psutil Basic usage
First, use dir(psutil) to see available methods, filtering out rarely used ones.
The output shows a manageable number of commands, many of which are familiar.
Examples:
Printing the current user information.
Listing all process IDs (pids) in the system.
With a pid you can locate the corresponding program directory.
To retrieve a process object, use the process method. For example, selecting pid 684:
Then you can call methods such as: pp.name() which returns the process name.
You can also list all process names and their paths:
Conversely, you can find all pids associated with a given process name:
Additional useful methods include:
pp.children(recursive=True) # list child processes
pp.cmdline() # command line arguments
pp.connections(kind='inet') # open socket connections
pp.cpu_affinity(cpus=None) # get/set CPU affinity
pp.cpu_percent(interval=None) # CPU usage percent
pp.cpu_times() # CPU times (user, system, ...)
pp.create_time() # process creation time
pp.cwd() # current working directory
pp.environ() # environment variables
pp.exe() # executable path
pp.kill() # kill process
pp.username() # process owner name
pp.ppid() # parent pid
pp.open_files() # opened files
pp.resume() # resume threads
pp.status() # current status
pp.num_threads() # number of threads
pp.threads() # thread info
pp.pid # process id
pp.terminal() # terminal
pp.terminate() # terminate processThere are many more methods you can explore in the documentation.
For example, methods prefixed with cpu, disk, or net provide detailed system metrics, such as CPU core count and disk usage.
One particularly impressive method, test(), can simulate a Task Manager‑like view of processes.
Finally, a small script can print all process IDs, names, and paths, handling permission errors with conditional checks.
In summary, psutil provides a comprehensive set of tools for accessing process information and system metrics, and with the examples above you can quickly start building your own monitoring utilities.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Python Crawling & Data Mining
Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
