If all you're trying to do is test whether a process is running by its process name, you could just import the check_output method from the subprocess module (not the entire module):
from subprocess import check_output
print('Test whether a named process appears in the task list.\n')
processname = input('Enter process name: ') # or just assign a specific process name to the processname variable
tasks = check_output('tasklist')
if processname in str(tasks):
print('{} is in the task list.'.format(processname))
else:
print('{} not found.'.format(processname))
Output:
>>> Discord.exe
Discord.exe is in the task list.
>>> NotARealProcess.exe
NotARealProcess.exe not found.
(This works for me on Windows 10 using Python 3.10.) Note that since this is just searching for a specific string across the entire task list output, it will give false positives on partial process names (such as "app.exe" or "app" if "myapp.exe" is running) and other non-process text input that happens to be in the task list:
>>> cord.ex
cord.ex is in the task list.
>>> PID Session Name
PID Session Name is in the task list.
This code generally should work fine if you just want to find a known process name in the task list and are searching by the whole name, but for more rigorous uses, you might want to use a more complex approach like parsing the task list into a dictionary and separating out the names for more focused searching, as well as add some error checking to handle edge cases.