pythonprocesspyinstallerwindowed

Is there a way to get a list of processus with PyInstaller windowed?


I'm trying to test if a browser is open on a python program converted to an exe with pyinstaller, Is there a way to test this without displaying a window and converting it to an exe?

I tried subprocess but it creates an error with the windowed mode of pyinstaller and I can't import psutil into pyinstaller ("can't import psutil" error)

The code I use with subprocess:

enter code heresubprocess.check_output('tasklist', shell=True)

The program does not start and an error message appears: "Failed to execute script" A similar error posted here: subprocess seems not working in pyinstaller exe file

Thank you in advance for your answers


Solution

  • If you just want to check whether a process running or not you can use psutil.process_iter but as Pyinstaller can't quite resolve the psutil module you need to use add-data flag to add the whole Lib folder to your output executable:

    import psutil
    process_to_find = "chrome.exe"
    process_list = [p.name() for p in list(psutil.process_iter())]
    if process_to_find in process_list:
       # do whatever you want
       print("Process found!")
    

    Next use below command to generate your executable (you can also use -w flag as well):

    pyinstaller -F --add-data "<python_path>\Lib\site-packages\psutil;./psutil" script.py