How can I make my console hide while executing a script? I want to make a separate function to do it (maybe by applying some methods from os
/sys
, I don't know), so I do not need some solutions like changing script extension from .py
to .pyw
.
-
What OS are you using? – martineau Mar 29 '16 at 20:32
-
@martineau, Windows 8, as a consequence adivce like nohup or & don`t make sense – Akado2009 Mar 29 '16 at 20:33
-
I hope you can't do that. What if someone runs the script in a terminal emulator with other tabs open? Would the other tabs also be hidden? – Stop harming Monica Mar 29 '16 at 20:42
-
1You can hide the console using ctypes, with `kernel32.GetConsoleWindow` and `user32.ShowWindow`, but you should only hide a new console. If `kernel32.GetConsoleProcessList` has more than py.exe and python.exe, don't hide the window unless you want to make someone hate you. – Eryk Sun Mar 30 '16 at 00:41
3 Answers
If you want to hide the window during execution (and your script is for Windows only) then ctypes may be a possibility for you.
Using this answer you could take input and then hide it:
import ctypes
a = input('Input value here:')
kernel32 = ctypes.WinDLL('kernel32')
user32 = ctypes.WinDLL('user32')
SW_HIDE = 0
hWnd = kernel32.GetConsoleWindow()
user32.ShowWindow(hWnd, SW_HIDE)
# Do stuff here
This is running off C ShowWindow()
. You can get more (indepth) info from the Windows's own documentation.

- 11,201
- 10
- 62
- 89
-
1Works fine, I have a problem with an application running as a service in windows using the os library, so I change it to a normal execution with this and my problem was solved. – Josete Manu Dec 04 '19 at 13:34
-
Great! I needed to hide the console window of an .exe created with cx_freeze depending on the command line parameters provided to the script. While the console window is displayed for a brief moment as a result of the script starting before evaluating the code which hides the window, this is an acceptable tradeoff for the ability to hide the console window dynamically. Any other approach (like a wrapper that will start a hidden subprocess.Popen() depending on the provided parameters) are more complex and thus less comprehensible for other contributors to my codebase. – M463 May 18 '22 at 13:23
The way I've done it on Windows 7 is by making a shortcut (or link) that runs my script with the pythonw.exe
interpreter, which has no console, instead of the default python.exe
.
Just follow these 3 steps:
- First create a normal shortcut to your script. One way to do this is to drag the icon for the script file shown in an Explorer window to where you want the shortcut to be (like the desktop) and hold down the Alt key when releasing the mouse button.
- Right-click on the just created shortcut and select
Properties
from the the menu that pops-up. - A Properties dialog for the shortcut will appear. In it insert
C:\python27\pythonw.exe
and a space before the path to the your script file. if the path to your script has any spaces in it, it must now be enclosed in double quotes._ If you're using another version of Python, you'll also need to change thePython27
accordingly.
i.e. A target of D:\path with spaces in it to\myscript.py
would need to be changed
to C:\Python27\pythonw.exe "D:\path with spaces in it to\myscript.py"
You can also change the shortcut's icon here if you wish.
Here's an example:
Update - simpler way: You can also change the extension of your script to .pyw
which will cause it to be run with the pythonw.exe
instead of the python.exe
.

- 119,623
- 25
- 170
- 301
-
1For anyone getting here through a web search, minimizing a window is not the same thing as hiding it. So if you're actually looking to hide the console, see my comment on the question itself. I don't have anything against the advice in this answer, but I do think it's better suited to superuser.com. Clearly the OP is satisfied with minimizing the console window, but this answer would be better (and more on topic) if it showed how to use `win32com` to create the shell shortcut. – Eryk Sun Mar 30 '16 at 13:36
-
@eryksun: Actually creating a shortcut that runs something minimized isn't really the secret sauce (or even necessary). The real trick is making one to run `pythonw.exe` — which has no console — on a specific Python script. So in that sense, my answer is misleading. Using `win32com` to create a shortcut wouldn't work because the script doing that would itself have a console...so it appears to be a proverbial chicken-and-egg problem. It's unclear to me how something like this could be done within the script itself because by then, it's too late. – martineau Mar 30 '16 at 14:43
-
I didn't notice that you used pythonw.exe. I had thought the OP didn't want to go that route because using .pyw was ruled out, which (assuming the system is configured correctly) is effectively the same. But instead of explicitly passing the command line, using .pyw has `ShellExecuteEx` look up the .pyw association and run the script with pythonw.exe (or pyw.exe). – Eryk Sun Mar 30 '16 at 15:58
-
You can hide the console window, but it will flash on the screen briefly at first since Windows creates the console with a visible window. Unfortunately nothing in the user interface allows controlling the creation flags such as `CREATE_NO_WINDOW` or `DETACHED_PROCESS`. All you can do is start it minimized or use a non-console executable such as pythonw.exe. – Eryk Sun Mar 30 '16 at 16:04
-
@eryksun: You're just reinforcing what I meant about it being a chicken-and-egg problem. I have a different idea, which I'll explore and may post another answer depending on what I discover. – martineau Mar 30 '16 at 16:51
-
user3570029: I changed my answer quite a bit, so feel free to un-accept it if it doesn't address your problem anymore. – martineau Mar 30 '16 at 20:19
You can run a python program in the background by adding a & at the end:
python myfile.py &
If you want to be able to close out your console and have the process run in the background, you might want to check out nohup:

- 41
- 2