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


Posted in Python onNovember 29, 2018

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

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的版本,选择了 pyenv 这个管理工具,是一个独立出来的python环境。

尝试解决无果

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

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

>>> 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 包!

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社区:https://docs.python.org/3/library/tkinter.html ,这才懂了他是啥玩意:

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

这时候在运行你的代码就可以了。

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python使用urllib2模块实现断点续传下载的方法
Jun 17 Python
python中利用Future对象异步返回结果示例代码
Sep 07 Python
为什么入门大数据选择Python而不是Java?
Mar 07 Python
使用NumPy和pandas对CSV文件进行写操作的实例
Jun 14 Python
程序员写Python时的5个坏习惯,你有几条?
Nov 26 Python
python全栈要学什么 python全栈学习路线
Jun 28 Python
numpy 声明空数组详解
Dec 05 Python
python自动化unittest yaml使用过程解析
Feb 03 Python
详解pycharm2020.1.1专业版安装指南(推荐)
Aug 07 Python
vscode+PyQt5安装详解步骤
Aug 12 Python
20行代码教你用python给证件照换底色的方法示例
Feb 05 Python
Python中对象的比较操作==和is区别详析
Feb 12 Python
pycharm 取消默认的右击运行unittest的方法
Nov 29 #Python
selenium+python设置爬虫代理IP的方法
Nov 29 #Python
Pycharm取消py脚本中SQL识别的方法
Nov 29 #Python
利用python GDAL库读写geotiff格式的遥感影像方法
Nov 29 #Python
在python中利用GDAL对tif文件进行读写的方法
Nov 29 #Python
使用python判断你是青少年还是老年人
Nov 29 #Python
pycham查看程序执行的时间方法
Nov 29 #Python
You might like
谈一谈收音机的高放电路
2021/03/02 无线电
编写PHP脚本使WordPress的主题支持Widget侧边栏
2015/12/14 PHP
Laravel5.1数据库连接、创建数据库、创建model及创建控制器的方法
2016/03/29 PHP
JavaScript加密解密7种方法总结分析
2007/10/07 Javascript
javascript代码编写需要注意的7个小细节小结
2011/09/21 Javascript
js获取location.href的参数实例代码
2013/08/02 Javascript
javascript自动给文本url地址增加链接的方法分享
2014/01/20 Javascript
jquery form 加载数据示例
2014/04/21 Javascript
JQuery中节点遍历方法实例
2015/05/18 Javascript
JavaScript中数组继承的简单示例
2015/07/29 Javascript
在JavaScript中call()与apply()区别
2016/01/22 Javascript
老生常谈JavaScript数组的用法
2016/06/10 Javascript
jQuery实现鼠标滑过图片移动特效
2016/12/08 Javascript
vue配置请求本地json数据的方法
2018/04/11 Javascript
浅谈node中的cluster集群
2018/06/02 Javascript
layui表格 返回的数据状态异常的解决方法
2019/09/10 Javascript
使用uni-app开发微信小程序的实现
2019/12/13 Javascript
Python 字符串操作方法大全
2014/03/11 Python
Python编程入门的一些基本知识
2015/05/13 Python
Python使用正则表达式实现文本替换的方法
2017/04/18 Python
Python使用win32com实现的模拟浏览器功能示例
2017/07/13 Python
python脚本实现验证码识别
2018/06/07 Python
selenium + python 获取table数据的示例讲解
2018/10/13 Python
PyQt4实时显示文本内容GUI的示例
2019/06/14 Python
python连接mysql数据库并读取数据的实现
2020/09/25 Python
python如何调用百度识图api
2020/09/29 Python
移动端html5 meta标签的神奇功效
2016/01/06 HTML / CSS
CAT鞋美国官网:CAT Footwear
2017/11/27 全球购物
Pedro官网:新加坡时尚品牌
2019/08/27 全球购物
extern是什么意思
2016/03/10 面试题
幼儿园毕业典礼主持词
2014/03/21 职场文书
幼儿园小班家长寄语
2014/04/02 职场文书
《爱的教育》读书心得
2014/11/08 职场文书
事业单位工作人员年度考核个人总结
2015/02/12 职场文书
2015年教师节贺卡寄语
2015/03/24 职场文书
道歉短信大全
2015/05/12 职场文书