pyenv下使用python matplotlib模块的问题解决

错误信息

先来描述一下我遇到的问题,在进行matplotlib学习时,plot.show()老是没法成功运行,老是会报一个错:html

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.python

其实意思很简单,就是我用的python并非一个做为系统框架存在的,由于我为了方便管理python的版本,选择了pyenv这个管理工具,是一个独立出来的python环境。linux

尝试解决无果

参考网上众多的解决方法,例如如下两个最多见的:bash

方法一: 添加以下两行 代码解决:app

>>> import matplotlib
>>> matplotlib.use('TkAgg')
##在import matplotlib下的模块,如pyplot等以前添加上面2句
>>> import matplotlib.pyplot as plt
复制代码

方法二: 添加一下matplotlib的配置:框架

echo "backend: TkAgg" >> ~/.matplotlib/matplotlibrc
复制代码

然而,以上这两种解决方式都***没法解决个人问题***,此时出现了第二个错误:工具

No module named '_tkinter'学习

说是找不到tkinter这个模块,找了网上大多数方法,全都是linux系统下的解决方案,我真的很好奇没有一个使用mac的用户出现我这样的问题吗? 究其缘由,是由于,使用pyenv独立安装出来的python中并无tkinter这个模块,因而尝试直接安装tkinter,结果居然提示没有发现tkinter包!ui

pip3 install tkinter
Collecting tkinter
Could not find a version that satisfies the requirement tkinter (from versions: )
No matching distribution found for tkinter
复制代码

来到这,我不由陷入了深深的思考,这个tkinter究竟是何方神圣,去了Python社区:docs.python.org/3/library/t…,这才懂了他是啥玩意:spa

The tkinter package (“Tk interface”) is the standard Python interface to the Tk GUI toolkit. Both Tk and tkinter are 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.

说白了,tkinter 就是一个利用python作GUI(图形用户界面),它提供各类标准的 GUI 接口项,以利于迅速进行高级应用程序开发。

那么究竟去哪安装这个tkinter包,说实话到如今我也不知道如何利用pyenv去安装tkinter,那这个问题又该怎么解决呢?

曲线救国

既然tkinter这个GUI库没用,那换个库是否是就行了呢?结果的确和我想的同样,在我换了一个GUI库以后,他的确成功了。 具体操做以下: 在出现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.这个错误的时候,在终端输入如下命令:

echo "backend : Qt5Agg" > ~/.matplotlib/matplotlibrc
复制代码

若是提示你没有安装PyQt的话,你就须要执行

brew install pyqt
复制代码

而后在执行

pip install PyQt5
复制代码

这时候在运行你的代码就能够了。

相关文章
相关标签/搜索