I have packaged my python application into a standalone executable with pyinstaller and every time subprocess.run
is called, a terminal pops up, then disappears when the command finished executing. This can be quite distracting when dozens of terminals spawn in quick succession. How do I keep these terminals from showing?
script.py
import subprocess
command = ["powershell", "-Command", "Get-ChildItem"]
subprocess.run(command)
PS> pyinstaller script.py
double click on dist\script\script.exe
subprocess.run
instantiates Popen
and forwards *popenargs
and **kwargs
to Popen
. The keyword argurment startupinfo
has some flags that can be used to suppress displaying the terminal window. Setting these flags in startupinfo
successfully hides the windows.
import subprocess
hide_window = subprocess.STARTUPINFO()
hide_window.dwFlags |= subprocess.STARTF_USESHOWWINDOW
hide_window.wShowWindow = subprocess.SW_HIDE
command = [ ... ]
subprocess.run(command, startupinfo=hide_window)