Apparently Python (or at least my python v3.11.2, macOS v12.6.3 system) lets you iterate through all the OS/system's processes, but prevents you from reading the cmdline of all the processes, even if it lets you read the "base name" of the process of cmdline-problematic processes. More details.
So catch and bypass the exception:
try:
cmdline = proc.cmdline()
except psutil.AccessDenied:
continue
Full, working script (on my above macOS system):
#!/usr/bin/env python3
import psutil, logging
log = logging.getLogger()
# psutil all-processes iteration:
# https://stackoverflow.com/a/4230226/605356
for proc in psutil.process_iter():
# avoid erroring on proc.cmdline() + psutil.AccessDenied Exception,
# even though proc.name() experiences no issue:
# https://www.florianreinhard.de/2022-02-28/accessdenied-in-psutil/
try:
cmdline = proc.cmdline()
except psutil.AccessDenied:
continue
except (psutil.ZombieProcess, psutil.NoSuchProcess):
# these Exceptions also happen a lot on my
# macOS v12.6.3, python v3.11.2 system
continue
except Exception as e: # optional and use with care;
# see https://stackoverflow.com/q/4990718/605356
log.exception("something is wrong: " + ' '.join(cmdline))
continue
print('name = ' + proc.name())
print('cmdl = ' + ' '.join(cmdline))