Search code examples
pythonpython-3.xsubprocessadbcx-freeze

Python subprocess command to run silent, prevent cmd from appearing


I have some complicated Python3 GUI code with tinker, and compiled with cx_Freeze.

The issue only occurs when run on Windows.

subprocess check_ouptut (or Popen) runs a similar command:

import subprocess
VAL = subprocess.check_output(['adb.exe', 'version'], shell=False, stdout=subprocess.PIPE).decode()

So I need to capture the output and store it as VAL. However, when it happens, the cmd window pops up, and closes after the value was read. I have a set of commands those do similar thing, and it results in the adb.exe popping up in a cmd window, which is really annoying.

Is there a way to make these silent, so the cmd does NOT pop up?

As I mentioned, the code is run as GUI/tkinter, compiled with cx_Freeze, and only occurs on Microsoft Windows (does not happen on Linux).

Thank you.


Solution

  • My solution was:

    import subprocess,os
    startupinfo = None
    if os.platform == 'win32':
        startupinfo = subprocess.STARTUPINFO()
        startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
    VAL = subprocess.check_output(['adb.exe', 'version'], shell=False, startupinfo=startupinfo).decode()