pythonw.exe or python.exe?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
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. Use pythonw.exe for GUI applications and background processes where a console window would be unwanted. The .pyw file extension is the file-association equivalent of pythonw.exe, so double-clicking a .pyw file launches it without a console.
How They Differ Internally
Both executables run the same Python interpreter and execute the same bytecode. The only difference is in how Windows handles the process at the OS level.
python.exe is linked as a console application (the SUBSYSTEM flag in the PE header is set to CONSOLE). When Windows launches a console application, it either attaches it to the existing console or creates a new console window. This is why running a Python script from Explorer by double-clicking a .py file briefly flashes a black window.
pythonw.exe is linked as a Windows GUI application (SUBSYSTEM is set to WINDOWS). Windows does not create or attach a console window for GUI subsystem processes. The standard streams (sys.stdin, sys.stdout, sys.stderr) are set to None.
When to Use Each
| Scenario | Use | Why |
| CLI tools and scripts | python.exe | You need to see output and interact with stdin |
| Debugging any script | python.exe | Error messages and stack traces appear in the console |
| GUI apps (tkinter, PyQt, wxPython) | pythonw.exe | A console window behind the GUI is distracting |
| Background services / tray apps | pythonw.exe | No visible console window needed |
| Scheduled tasks (Task Scheduler) | pythonw.exe | Avoids a console window popping up on schedule |
| Jupyter notebooks | python.exe | The kernel needs console I/O |
The .py vs .pyw File Extension
Windows file associations connect .py files to python.exe and .pyw files to pythonw.exe. This means the file extension controls which executable runs when you double-click a script in Explorer.
From the command line, you can always choose explicitly:
The file extension does not matter when you invoke the interpreter directly. It only matters for double-click behavior.
The stdout Problem with pythonw.exe
Because pythonw.exe does not attach a console, sys.stdout and sys.stderr are None. Any print() call or unhandled exception that tries to write to these streams will raise an AttributeError or silently disappear, depending on the Python version.
This is the most common source of confusion. A script works perfectly with python.exe but silently fails with pythonw.exe.
Solution: redirect output to a log file
Solution: use a NullWriter for silent operation
If you do not care about output at all:
A Practical GUI Example
Here is a minimal tkinter application. Running it with pythonw.exe gives a clean GUI window without a console lurking behind it:
Save this as app.pyw and double-click it. The GUI appears with no console window.
The Python Launcher (py.exe)
On Windows, the Python Launcher (py.exe) was introduced to handle multiple Python versions. It reads shebang lines and version specifiers:
The launcher respects the .py / .pyw extension convention and can be configured via py.ini for custom defaults.
Task Scheduler and Services
When setting up a scheduled task in Windows Task Scheduler, use pythonw.exe as the program to avoid a console window popping up every time the task fires:
For Windows services, consider using a dedicated service wrapper like pywin32's win32serviceutil or NSSM, which handle process lifecycle management beyond what pythonw.exe alone provides.
Comparison Table
| Feature | python.exe | pythonw.exe |
| Console window | Yes (created or attached) | No |
| sys.stdout | Connected to console | None |
| sys.stderr | Connected to console | None |
| sys.stdin | Connected to console | None |
| PE subsystem | CONSOLE | WINDOWS |
| File association | .py | .pyw |
| Use case | CLI, debugging, scripts | GUI apps, background tasks |
| Error visibility | Errors print to console | Errors are silent unless logged |
Common Pitfalls
Using pythonw.exe for debugging. When sys.stderr is None, exceptions vanish silently. You will not see any error output. Always develop and debug with python.exe, then switch to pythonw.exe for deployment.
Calling print() without guarding stdout. print() writes to sys.stdout, which is None under pythonw.exe. Either redirect to a file, use the logging module, or wrap sys.stdout with a null writer.
Assuming .pyw changes the script behavior. The extension only affects which executable Windows uses when double-clicking the file. The Python code inside runs identically. If you invoke python.exe script.pyw, it will still open a console.
Running a Flask/Django dev server with pythonw.exe. Web framework dev servers produce output on stdout and expect console interaction for shutdown. Use python.exe for development servers.
Forgetting to flush log files. When redirecting output to files under pythonw.exe, buffering may delay writes. Use flush=True in print() calls or configure logging handlers with flush behavior.
Summary
python.execreates a console window and connects standard I/O streams. Use it for command-line tools, scripts, and all development/debugging work.pythonw.exesuppresses the console window and setssys.stdout/sys.stderrtoNone. Use it for GUI applications, background processes, and scheduled tasks.- The
.pywfile extension tells Windows to usepythonw.exewhen double-clicking the script. - Always add logging or output redirection to scripts intended for
pythonw.exe, because errors will be invisible without it. - The Python Launcher (
py.exe/pyw.exe) provides version selection and respects the same console/no-console distinction. - Develop with
python.exe. Deploy withpythonw.exewhen a console-free experience is needed.
Related reading
- pytorch Network.parameters missing 1 required positional argument 'self
- Query DynamoDB with a hash key and a range key with Boto3
- Query whether Python's threading.Lock is locked or not
- queue.Queue vs. collections.deque
- Quicksort with Python
- RabbitMQ def callback(ch, method, properties, body)
- RabbitMQ How to send Python dictionary between Python producer and consumer?
- RabbitMQ, Pika and reconnection strategy
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.