The error message

To describe the problem I had with learning matplotlib, plot.show() always failed to run and always reported an error:

RuntimeError: Python is not installed as a framework. The Mac OS X backend will not be able to function correctly if Python is not installed as a framework.See the Python documentation for more information on installing Python as a framework on Mac OS X. Please either reinstall Python as a framework, or try one of the other backends. If you are using (Ana)Conda please install python.app and replace the use of ‘python’ with ‘pythonw’. See ‘Working with Matplotlib on OSX’ in the Matplotlib FAQ for more information.

The python I use does not exist as a system framework, because I use pyenv as a management tool to facilitate the management of python versions, which is a separate Python environment.

Attempts to solve the problem were fruitless

Refer to the numerous solutions available online, such as the following two most common ones:

Method 1: Add the following two lines of code to solve the problem:

>>> import matplotlib
>>> matplotlib.use('TkAgg')
## Add the above two sentences before importing modules under Matplotlib such as Pyplot
>>> import matplotlib.pyplot as plt
Copy the code

Method 2: Add the following configuration for Matplotlib:

echo "backend: TkAgg" >> ~/.matplotlib/matplotlibrc
Copy the code

However, both solutions failed to solve my problem ***, and a second error occurred:

No module named ‘_tkinter’

I said I couldn’t find the module tkinter. I found most methods on the Internet, all of which were solutions under Linux system. I am really curious that no MAC users have the same problem as me? The tkinter package is not found in python installed with Pyenv. The tkinter package is not found in Python.

pip3 install tkinter
Collecting tkinter
Could not find a version that satisfies the requirement tkinter (from versions: )
No matching distribution found for tkinter
Copy the code

Come to this, I can’t help but fell into deep thinking, the tkinter exactly is who, went to the Python community: docs.python.org/3/library/t… And now I know what he is:

The Tkinter Package (” Tk Interface “) is The standard Python interface to The Tk GUI Toolkit available on most Unix platforms, as well as on Windows systems. (Tk itself is not part of Python; it is maintained at ActiveState.) Running python -m tkinter from the command line should open a window demonstrating a simple Tk interface, letting you know that tkinter is properly installed on your system, and also showing what version of Tcl/Tk is installed, so you can read the Tcl/Tk documentation specific to that version.

To put it bluntly, Tkinter is a Python GUI(graphical user interface) that provides a variety of standard GUI interface items for rapid advanced application development.

How can I install tkinter with PyEnv?

Curve of national salvation

Since the Tkinter GUI library is useless, why not change it to another library? It worked out exactly as I thought, after I switched to a GUI library. Specific operations are as follows: Python is not installed as a framework. The Mac OS X backend will not be able to function correctly if Python is not installed as a framework. When this error occurs, type the following command on the terminal:

echo "backend : Qt5Agg" > ~/.matplotlib/matplotlibrc
Copy the code

If you are prompted not to install PyQt, you will need to do so

brew install pyqt
Copy the code

And then execute

pip install PyQt5
Copy the code

Just run your code at this point.