5

Consider the following simple python code:

import matplotlib.pyplot as mplot 
mplot.plot([1,2,3,4],[1,2,3,4])

This script has no problems when the script runs, however if I close the python console (by clicking the red x), I get the following error:

Fatal Python error: PyEval_RrestoreThread: NULL tstate

This problem does not occur if I use ctrl+Z to exit the python console. It seems to me that there is some hanging process that isn't terminated properly if I exist the first way. I've attempted adding

mplot.close('all')

to the end of the script, but I get the following resulting error:

can't invoke "event" command: application has been destroyed while executing 
"event generate $w <<ThemeChanged>>"
(procedure "ttk::ThemeChanged" line 6)
invoke from within
"ttk::ThemeChanged"

I am running Python 3.3 x86. Could anyone help me understand this issue?

Thanks!

Sergiy
  • 1,029
  • 1
  • 12
  • 19

3 Answers3

5

The issue is that the standard Python interpreter isn't built with knowledge of how to handle graphical user interfaces. Without going into detail it looks like matplotlib was trying to close down a thread that doesn't exist.

There's an explanation of the causes at http://matplotlib.org/users/shell.html and pointers to discussions about workarounds.

The simplest answer is to use IPython, which is matplotlib aware as the reference explains. If this isn't an option you may have to dig deeper.

holdenweb
  • 33,305
  • 7
  • 57
  • 77
  • Thanks a lot! That's a great resource. – Sergiy Feb 27 '14 at 21:14
  • 1
    Thanks holdenweb. I had a similar issue: upon calling plt.close() when using Tkinter GUI from a command line scirpt, I would get `can't invoke 'event' command`. I downloaded the [matplotlibrc file](https://matplotlib.org/_static/matplotlibrc) from [here](https://matplotlib.org/tutorials/introductory/customizing.html#sphx-glr-tutorials-introductory-customizing-py), placed it in my script folder and changed `interactive : True` The error was gone afterwards! – Alex Dec 21 '17 at 09:50
3

Short answer: make sure you call plt.close('all').


I had the same issue today. This would arise during an edge case where the code would decide not to generate a graph and would exit a function early, without calling plt.close('all') (where plt comes from import matplotlib.pyplot as plt).

I resolved it quickly by just adding the plt.close('all') during the edge case.

StaringFrog
  • 561
  • 7
  • 21
0

This happened to me. None of the posted answers fixed the problem. I ignored the message and everything is working as expected.

kilojoules
  • 9,768
  • 18
  • 77
  • 149