pythonw.exe
python.exe
Python scripting
command line
Windows Python

pythonw.exe vs python.exe, which one to use

Codemia Knowledge Hub ·

python.exe runs Python scripts with a console window attached. pythonw.exe runs them without one. Use python.exe for command-line tools, scripts and debugging, and pythonw.exe for GUI applications and background processes where a console window is unwanted. Double-clicking a .pyw file launches it with pythonw.exe, so no console appears.

What differs at the OS level

Both executables run the same interpreter and the same bytecode. The difference is how Windows treats the process.

python.exe is linked as a console application (the SUBSYSTEM flag in the PE header is CONSOLE). Windows either attaches it to an existing console or creates a new console window, which is why double-clicking a .py file in Explorer briefly flashes a black window.

pythonw.exe is linked as a Windows GUI application (SUBSYSTEM is WINDOWS). Windows does not create or attach a console, and sys.stdin, sys.stdout and sys.stderr are set to None.

text
python.exe   -> SUBSYSTEM:CONSOLE  -> console window attached
pythonw.exe  -> SUBSYSTEM:WINDOWS  -> no console window
Featurepython.exepythonw.exe
Console windowYes (created or attached)No
sys.stdoutConnected to consoleNone
sys.stderrConnected to consoleNone
sys.stdinConnected to consoleNone
PE subsystemCONSOLEWINDOWS
File association.py.pyw
Use caseCLI, debugging, scriptsGUI apps, background tasks
Error visibilityErrors print to consoleErrors are silent unless logged

Which one to use

ScenarioUseWhy
CLI tools and scriptspython.exeYou need to see output and interact with stdin
Debugging any scriptpython.exeError messages and stack traces appear in the console
GUI apps (tkinter, PyQt, wxPython)pythonw.exeA console window behind the GUI is distracting
Background services / tray appspythonw.exeNo visible console window needed
Scheduled tasks (Task Scheduler)pythonw.exeAvoids a console window popping up on schedule
Jupyter notebookspython.exeThe kernel needs console I/O

.py vs .pyw file extensions

Windows associates .py with python.exe and .pyw with pythonw.exe, so the extension decides which executable runs on double-click.

text
script.py   -> double-click -> runs with python.exe  (console appears)
script.pyw  -> double-click -> runs with pythonw.exe (no console)

From the command line you choose explicitly, and the extension does not matter:

cmd
python.exe script.py
pythonw.exe script.py

Why print() fails under pythonw.exe

With no console, sys.stdout and sys.stderr are None. A print() call or unhandled exception that writes to them raises an AttributeError or silently disappears, depending on the Python version. This is the usual reason a script works with python.exe but silently fails with pythonw.exe.

Redirect output to a log file

python
1import sys
2import logging
3
4# Configure logging to a file
5logging.basicConfig(
6    filename="app.log",
7    level=logging.INFO,
8    format="%(asctime)s %(levelname)s %(message)s",
9)
10
11# Redirect stdout/stderr so print() doesn't crash
12if sys.stdout is None:
13    sys.stdout = open("app_stdout.log", "w")
14if sys.stderr is None:
15    sys.stderr = open("app_stderr.log", "w")
16
17logging.info("Application started")
18print("This goes to app_stdout.log when run with pythonw.exe")

Discard output with a null writer

If you do not need the output at all:

python
1import sys
2import os
3
4class NullWriter:
5    def write(self, s):
6        pass
7    def flush(self):
8        pass
9
10if sys.stdout is None:
11    sys.stdout = NullWriter()
12if sys.stderr is None:
13    sys.stderr = NullWriter()

A tkinter app without a console

python
1# save as app.pyw
2import tkinter as tk
3
4def on_click():
5    label.config(text="Button clicked!")
6
7root = tk.Tk()
8root.title("Example App")
9root.geometry("300x150")
10
11label = tk.Label(root, text="Hello from pythonw.exe", font=("Arial", 14))
12label.pack(pady=20)
13
14button = tk.Button(root, text="Click me", command=on_click)
15button.pack()
16
17root.mainloop()

Save it as app.pyw and double-click it. The GUI appears with no console window behind it.

The Python Launcher (py.exe and pyw.exe)

The Windows Python Launcher handles multiple Python versions. It reads shebang lines and version specifiers:

cmd
py script.py         # runs with default python.exe
pyw script.py        # runs with default pythonw.exe
py -3.11 script.py   # runs with Python 3.11's python.exe

It follows the same .py / .pyw convention and can be configured through py.ini for custom defaults.

Task Scheduler and Windows services

In Task Scheduler, use pythonw.exe as the program so no console window pops up every time the task fires:

text
Program/script:    C:\Python312\pythonw.exe
Add arguments:     C:\scripts\backup.py
Start in:          C:\scripts

For Windows services, use a dedicated wrapper such as pywin32's win32serviceutil or NSSM, which handle process lifecycle management beyond what pythonw.exe provides.

Mistakes with pythonw.exe

Debugging with it. With sys.stderr set to None, exceptions vanish. Develop and debug with python.exe, then switch to pythonw.exe for deployment.

Calling print() without guarding stdout. Redirect to a file, use logging, or install a null writer.

Assuming .pyw changes script behavior. The extension only affects which executable Windows uses on double-click. python.exe script.pyw still opens a console.

Running a Flask or Django dev server with it. Dev servers write to stdout and expect console interaction for shutdown. Use python.exe.

Not flushing log files. Buffering can delay writes when output goes to files. Use flush=True in print() or configure logging handlers to flush.


Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Basis Lab
No course covers this one yet

Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.

Build my course
Data Structures & Algorithms practice on Codemia

Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.

Practice algorithms