Title: hide command prompt when using subprocess.Popen with shell=False on Windows
Type: enhancement Stage: patch review
Components: Library (Lib), Windows Versions: Python 3.8
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: eryksun, iMath, ohno, paul.moore, steve.dower, swgmma, tim.golden, zach.ware
Priority: normal Keywords: easy, patch

Created on 2017-04-16 17:26 by iMath, last changed 2021-10-19 03:01 by eryksun.

File name Uploaded Description Edit swgmma, 2020-03-01 09:35 Script to test if subprocess spawns additional windows
Pull Requests
URL Status Linked Edit
PR 3013 closed jeffreyrack, 2017-08-07 05:18
PR 19014 open swgmma, 2020-03-15 10:28
Messages (17)
msg291760 - (view) Author: Philip Lee (iMath) Date: 2017-04-16 17:26
First, It is nearly useless for the command prompt to pop up during  the running time of subprocess.Popen with shell=False.
Second, the popping up command prompt would interrupt users and do bad to user experience of GUI applications.
Third, I found QProcess within Qt won't pop up the command prompt  in using.

It would be convenient to add an argument to suppress the command prompt from popping up when using subprocess.Popen with shell=False on Windows, many users are missing the feature and these are many similar feature request questions like the following
msg291815 - (view) Author: Eryk Sun (eryksun) * (Python triager) Date: 2017-04-17 23:40
Hiding the console is required often enough that a keyword-only parameter for this would be useful. Other platforms could simply ignore it, in contrast to writing platform-dependent code that passes startupinfo. Note that this option would also hide the window of GUI programs that do not (intentionally) ignore SW_HIDE, such as notepad.
msg329979 - (view) Author: neel patel (ohno) Date: 2018-11-16 03:00
Can I work on this? I'm not sure of it's status, though.
msg330091 - (view) Author: Steve Dower (steve.dower) * (Python committer) Date: 2018-11-19 11:43
Looks like it's available.

It will be a new subprocess option to not create a new window. The underlying issue is that some applications always request a console, and if your own application doesn't have one then Windows will create it. Normally you would fix this in the target application, but we can offer an option to try and force any new windows to be hidden (though as Eryk points out, it may cause unexpected behaviour and so should not be the default).
msg362978 - (view) Author: (swgmma) * Date: 2020-02-29 12:29
I would like to pick up work on this (have started at link below), however I would like some help:

For testing purposes, could someone please provide a minimum working example of using subprocess that causes a command prompt to pop up?

I have not been able to reproduce it (using Python 3.7), though I vaguely remember encountering it in the past.
msg362979 - (view) Author: Steve Dower (steve.dower) * (Python committer) Date: 2020-02-29 12:35
If you use subprocess to launch any process that is marked SUBSYSTEM_CONSOLE, Windows will create a new console for it. For example, "python.exe" is such a process.

Eryk's answer in the very first StackOverflow link shows how to create a STARTUPINFO parameter with the correct settings. We just want to add a Boolean argument to subprocess.Popen.__init__() that can add that setting automatically.
msg363046 - (view) Author: (swgmma) * Date: 2020-03-01 09:35
Sorry, perhaps I did not word it clearly. Indeed the code to implement this looks trivial, however the part I am having trouble with is reproducing the reported issue in the first place.

For example, running the attached file from a command prompt (`python`) does not result in any additional windows popping up.

Either my test case is wrong or there is something else going on.
msg363060 - (view) Author: Steve Dower (steve.dower) * (Python committer) Date: 2020-03-01 14:10
Try running that script with pythonw.exe instead.
msg363116 - (view) Author: Philip Lee (iMath) Date: 2020-03-02 05:14
To reproduce the reported issue, one could also test with ffmpeg.exe
msg363143 - (view) Author: Steve Dower (steve.dower) * (Python committer) Date: 2020-03-02 09:25
It'll have to be a program that cannot inherit the active console. Ffmpeg normally can, but running the first script with pythonw will make sure there is no console to inherit, and a new one will pop up for a regular Python process.
msg364227 - (view) Author: (swgmma) * Date: 2020-03-15 10:21
> Try running that script with pythonw.exe instead.

That did the trick. Confirmed that the changes are working as intended.

Running the test script posted earlier and adding the new `force_hide` kwarg to the subprocess call:

With `force_hide=False`, a command prompt window pops up.
With `force_hide=True`, no command prompt window pops up.
msg403897 - (view) Author: (swgmma) * Date: 2021-10-14 11:47
Ping in case anyone from the windows team is available to review the PR
msg404094 - (view) Author: Steve Dower (steve.dower) * (Python committer) Date: 2021-10-16 16:25
Had a look. The change looks fine, just needs a NEWS entry, which I suggested as:

Adds new `force_hide` argument to :mod:`subprocess` functions. This passes ``SW_HIDE`` to the new process, which most applications will use to not display any window even if they normally would.
msg404095 - (view) Author: Steve Dower (steve.dower) * (Python committer) Date: 2021-10-16 16:26
That 'force_hide' in the NEWS shouldn't have single backticks. Make them ``double``, or the docs won't compile.
msg404122 - (view) Author: Eryk Sun (eryksun) * (Python triager) Date: 2021-10-16 23:27
The force_hide option could also force the use of a new hidden window, even if the current process has a console. That way the output is always hidden. For example:

    if force_hide or shell:
        if force_hide and not (creationflags & _winapi.DETACHED_PROCESS):
            creationflags |= _winapi.CREATE_NEW_CONSOLE
        startupinfo.dwFlags |= _winapi.STARTF_USESHOWWINDOW
        startupinfo.wShowWindow = _winapi.SW_HIDE

One can also use CREATE_NO_WINDOW to create a console that has no window, as opposed to a hidden window. For example:

    if force_no_window:
        if creationflags & _winapi.CREATE_NEW_CONSOLE:
            raise ValueError('force_no_window cannot be used with '
        creationflags |= _winapi.CREATE_NO_WINDOW
msg404261 - (view) Author: (swgmma) * Date: 2021-10-19 02:09
I implemented your first suggestion for `force_hide`.

Should we add your `force_no_window` suggestion as well?
msg404264 - (view) Author: Eryk Sun (eryksun) * (Python triager) Date: 2021-10-19 03:01
I was intending force_no_window as an alternative to adding force_hide, since CREATE_NO_WINDOW only affects console applications. Maybe a better name is force_hide_console.
Date User Action Args
2021-10-19 03:01:04eryksunsetmessages: + msg404264
2021-10-19 02:09:17swgmmasetmessages: + msg404261
2021-10-16 23:27:43eryksunsetmessages: + msg404122
2021-10-16 16:26:21steve.dowersetmessages: + msg404095
2021-10-16 16:25:46steve.dowersetmessages: + msg404094
2021-10-14 11:47:14swgmmasetmessages: + msg403897
2020-03-15 11:03:00swgmmasetpull_requests: - pull_request18362
2020-03-15 10:29:01swgmmasetpull_requests: + pull_request18362
2020-03-15 10:28:14swgmmasetkeywords: + patch
stage: needs patch -> patch review
pull_requests: + pull_request18361
2020-03-15 10:21:08swgmmasetmessages: + msg364227
2020-03-02 09:25:06steve.dowersetmessages: + msg363143
2020-03-02 05:14:07iMathsetmessages: + msg363116
2020-03-01 14:10:35steve.dowersetmessages: + msg363060
2020-03-01 09:35:15swgmmasetfiles: +

messages: + msg363046
2020-02-29 12:35:11steve.dowersetmessages: + msg362979
2020-02-29 12:29:32swgmmasetmessages: + msg362978
2020-02-29 05:46:59swgmmasetnosy: + swgmma
2018-11-19 11:43:20steve.dowersetmessages: + msg330091
versions: + Python 3.8, - Python 3.7
2018-11-16 03:00:40ohnosetnosy: + ohno
messages: + msg329979
2017-08-07 05:18:35jeffreyracksetpull_requests: + pull_request3046
2017-04-17 23:40:37eryksunsetcomponents: + Windows
versions: + Python 3.7
keywords: + easy
nosy: + paul.moore, tim.golden, eryksun, zach.ware, steve.dower

messages: + msg291815
stage: needs patch
2017-04-16 17:26:26iMathcreate