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.
| 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 |
Which one to use
| 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 |
.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.
From the command line you choose explicitly, and the extension does not matter:
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
Discard output with a null writer
If you do not need the output at all:
A tkinter app without a console
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:
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:
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.
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
System design interview problems on this topic
- Design a Web Crawler: solution and walkthrough
- Design a Task Scheduler: solution and walkthrough
- Design a URL Shortener (TinyURL): solution and walkthrough
- Design an API Rate Limiter: solution and walkthrough
.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 courseNo 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 courseData 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.