Run Python script without Windows console appearing

pythonw.exe will run the script without a command prompt. The problem is that the Python interpreter, Python.exe, is linked against the console subsystem to produce console output (since that's 90% of cases) -- pythonw.exe is instead linked against the GUI subsystem, and Windows will not create a console output window for it unless it asks for one.

This article discusses GUI programming with Python, and also alludes to pythonw.exe. It also helpfully points out that if your Python files end with .pyw instead of .py, the standard Windows installer will set up associations correctly and run your Python in pythonw.exe.

In your case it doesn't sound like a problem, but reliance upon pythonw.exe makes your application Windows-specific -- other solutions exist to accomplish this on, say, Mac OS X.


If you name your files with the ".pyw" extension, then windows will execute them with the pythonw.exe interpreter. This will not open the dos console for running your script.


I tried methods above, however, a console stills appears and disappears quickly due to a Timer in my script. Finally, I found following code:

import ctypes
import os
import win32process

hwnd = ctypes.windll.kernel32.GetConsoleWindow()      
if hwnd != 0:      
    ctypes.windll.user32.ShowWindow(hwnd, 0)      
    ctypes.windll.kernel32.CloseHandle(hwnd)
    _, pid = win32process.GetWindowThreadProcessId(hwnd)
    os.system('taskkill /PID ' + str(pid) + ' /f')

Quite easy in Win10:
Open a PowerShell and type this commands:

type nul > openWindowed.vbs
type nul > main.py
notepad openWindowed.vbs

Paste the next into your openWindowed.vbs file:

Set WshShell = CreateObject("WScript.Shell") 
WshShell.Run chr(34) & "C:\Users\path\to\main.py" & Chr(34), 0
Set WshShell = Nothing

The .vbs file will execute your main.py file without open a cmd. I use this a lot to the make.py files, because I don't know cmake scripting.


This will work on all Windows Versions:

1. Create "Runner.bat" file with Notepad (or any other text editor) and insert following content:

@echo off
python server.py

where server.py is the path of the Python script you want to run.

2. Create "RunScript.vbs" file with Notepad and insert following content:

CreateObject("Wscript.Shell").Run "runner.bat",0,True

3. Run the "RunScript.vbs" file with double click and your Python script will be runnig without any visible console windows

p.s. I know that this was not part of your question but it is often the case, if you want to run the script on windows start (after user login) just paste the shortcut of "RunScript.vbs" file into your startup folder. (On Windows 10 mostly under: C:\Users[USERNAME]\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup )

Best regards