[Resolved] Pythonw.exe has Stopped Working

Often, python developers face the challenging issue of resolving the error of “pythonw.exe has stopped working”. It consumes a lot of time in debugging the code. No Worries! We have got you covered as you will be able to find the answer to every question after going through this extract.

What is pythonw.exe?

Pythonw.exe has stopped working [Error Resolved]

Often, python developers dealing in GUI-based python projects face the error –“pythonw.exe has stopped working”. pythonw.exe is an application to launch GUI(Graphical User Interface) based on python scripts. pythonw.exe executes the files with the extension “.pyw” while the python.exe executes the files with the extension “.py“. While executing a file with python.exe, no console window opens to present the execution status. Here, the python program runs in the background and simply presents the output on a GUI application without letting the user know what has happened.

In simple words, execution of a program takes place without popping the DOS(Disk Operating System) console window, just execute that python program with the “.pyw” extension.

> pythonw <your_filename.pyw>

To sum up the introduction, “pythonw.exe”

  • it is asynchronous(Script runs in the background)
  • no console window opens

Causes Behind the Exception: pythonw.exe has stopped working

Python language encloses a vast range of packages and directories. One of those is the sys module.

This module provides access:

  • to some variables used or maintained by the interpreter
  • to functions that interact strongly with the interpreter. It is always available.

It consists of numerous functions. Out of those, three which are:

  • sys.stdin
  • sys.stdout
  • sys.stderr

These functions are used by Interpreters to handle input and output, and in displaying errors to the programmers.

All the functions pertaining to inputs are handled by stdin. This includes the calls to input() too.
stdout deals with the output for the print and expression statements 
All the error messages that interpreter prompts during execution of programme go to the stderr, which is displayed to the programmers.

pythonw.exe is devoid of these standard streams namely sys.stdin, sys.stdout, and sys.stderr. Hence, the errors compel the program to terminate.

  • In Python 2.x, when Interpreter tries to write to sys.stdout or sys.stderr ,the following exception occurs: 
    • IOError: [Errno 9] Bad file descriptor
    • pythonw.exe has stopped working

This is the result of Python 2.x invalid file descriptors

  • In Python 3.x, this is complemented with performing nothing when it finds that sys.stdout is None. Here, No exception or message pops, it simply stops working without any result.

Resolving the error: “pythonw.exe has stopped working”

Solving a problem includes two steps:

  1. Finding the actual problem
  2. Gathering the knowledge to solve it.
  • To find the source of the error, please add the following lines of code in the program file.
import sys, os
if sys.executable.endswith("pythonw.exe"):
  sys.stdout = open(os.devnull, "w");
  sys.stderr = open(os.path.join(os.getenv("TEMP"), "stderr-"+os.path.basename(sys.argv[0])), "w")

These lines will help the users by writing the errors in the “%TEMP%\stderr- <scriptFileName>

Since you have known the real cause of the exception, therefore, leverage your High Order Programming Skills (HOPS) to debug the code.

In this way we can solve the error: “python.exe has stopped working”

“Pythonw.exe has stopped working in Anaconda”

Anaconda is a Python distribution that bundles together a ton of packages. Conda environment contains a set of python libraries to execute the written code. But sometimes, the execution may come up with errors such as “pythonw.exe has stopped working”.

"Pythonw.exe has stopped working in Anaconda"

Usually, the errors encroach when there is a conflict between different versions of the python libraries. It can be solved by updating the environment packages to their latest form by the execution of the following commands.

conda update conda
conda update --update-all

Best practices to avoid crashing while using “pythonw.exe”

  • Avoid the usage of sys.stdin() and sys.stdout() functions such as print(),input(),etc.
  • Always add Methods to catch the exceptions that may happen.
  • Be extra cautious in writing code as a single mistake would crash the whole application, thereby wasting a lot of time in debugging.

[Bonus]: How to close pythonw when its running in background

One must refrain oneself from killing a background process because it may have sour repercussions. Still, the execution of the following commands in PowerShell or the terminal window will help you to do so.

For Windows:

taskkill /IM pythonw.exe /F

For MAC OS

killall pythonw

Bon codage!

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments