Cross-platform way to get PIDs by process name in python

You can use psutil (, which works on Windows and UNIX:

import psutil

PROCNAME = "python.exe"

for proc in psutil.process_iter():
    if == PROCNAME:

On my machine it prints:

<psutil.Process(pid=3881, name="python.exe") at 140192133873040>

EDIT 2017-04-27 – here’s a more advanced utility function which checks the name against processes’ name(), cmdline() and exe():

import os
import psutil

def find_procs_by_name(name):
    "Return a list of processes matching 'name'."
    assert name, name
    ls = []
    for p in psutil.process_iter():
        name_, exe, cmdline = "", "", []
            name_ =
            cmdline = p.cmdline()
            exe = p.exe()
        except (psutil.AccessDenied, psutil.ZombieProcess):
        except psutil.NoSuchProcess:
        if name == name_ or cmdline[0] == name or os.path.basename(exe) == name:
    return ls

Leave a Comment