Uruchamianie procesu w pythonw z Popen bez konsoli

Mam program z GUI, który uruchamia zewnętrzny program poprzez wywołanie Popen:

p = subprocess.Popen("<commands>" , stdout=subprocess.PIPE , stderr=subprocess.PIPE , cwd=os.getcwd())
p.communicate()

Ale pojawia się konsola, niezależnie od tego, co robię (próbowałem też podać NUL dla uchwytu pliku). Czy jest jakiś sposób, aby to zrobić bez uzyskania pliku binarnego, który wywołuję, aby uwolnić jego konsolę?

Author: sbirch, 2009-11-29

4 answers

From here :

import subprocess

def launchWithoutConsole(command, args):
    """Launches 'command' windowless and waits until finished"""
    startupinfo = subprocess.STARTUPINFO()
    startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
    return subprocess.Popen([command] + args, startupinfo=startupinfo).wait()

if __name__ == "__main__":
    # test with "pythonw.exe"
    launchWithoutConsole("d:\\bin\\gzip.exe", ["-d", "myfile.gz"])
 28
Author: interjay,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-11-28 21:58:35

To działa ładnie w win32api. Inne rozwiązania nie działały dla mnie.

import win32api
chrome = "\"C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe\""
args = "https://stackoverflow.com"

win32api.WinExec(chrome + " " + args)
 1
Author: Devon Dieffenbach,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-07-03 16:56:01

Just do subprocess.Popen([command], shell=True)

 1
Author: Liam,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-09-26 13:36:45

Być może będziesz w stanie po prostu zrobić subprocess.Popen([command], shell=False).

I tak tego używam. Oszczędza Ci cały nonsens ustawiania FLAG i tego typu rzeczy. Kiedyś nazwany .pyw lub uruchom z pythonw nie powinno otwierać konsoli.
 0
Author: ThantiK,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-11-28 23:27:55