详解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解析树及树的遍历
Feb 03 Python
Python自动化运维之IP地址处理模块详解
Dec 10 Python
Python切片工具pillow用法示例
Mar 30 Python
django的ORM操作 删除和编辑实现详解
Jul 24 Python
基于Python获取docx/doc文件内容代码解析
Feb 17 Python
python闭包、深浅拷贝、垃圾回收、with语句知识点汇总
Mar 11 Python
Python 执行矩阵与线性代数运算
Aug 01 Python
Python钉钉报警及Zabbix集成钉钉报警的示例代码
Aug 17 Python
详解numpy.ndarray.reshape()函数的参数问题
Oct 13 Python
python3.7中安装paddleocr及paddlepaddle包的多种方法
Nov 27 Python
解决Django transaction进行事务管理踩过的坑
Apr 24 Python
浅谈怎么给Python添加类型标注
Jun 08 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
php教程之phpize使用方法
2014/02/12 PHP
Yii 2.0在Grid中格式化时间方法示例
2017/06/06 PHP
thinkphp5框架实现数据库读取的数据转换成json格式示例
2019/10/10 PHP
JavaScript Cookie 直接浏览网站分网址
2009/12/08 Javascript
16个最流行的JavaScript框架[推荐]
2011/05/29 Javascript
得到form下的所有的input的js代码
2013/11/07 Javascript
微信小程序 两种滑动方式(横向滑动,竖向滑动)详细及实例代码
2017/01/13 Javascript
用JavaScript实现让浏览器停止载入页面的方法
2017/01/19 Javascript
微信小程序使用input组件实现密码框功能【附源码下载】
2017/12/11 Javascript
使用vue2实现带地区编号和名称的省市县三级联动效果
2018/11/05 Javascript
node.js学习笔记之koa框架和简单爬虫练习
2018/12/13 Javascript
详解无限滚动插件vue-infinite-scroll源码解析
2019/05/12 Javascript
解决微信小程序云开发中获取数据库的内容为空的方法
2019/05/15 Javascript
Vue 使用beforeEach实现登录状态检查功能
2019/10/31 Javascript
javascript 内存模型实例详解
2020/04/18 Javascript
vue pages 多入口项目 + chainWebpack 全局引用缩写说明
2020/09/21 Javascript
Python中的with...as用法介绍
2015/05/28 Python
python3.4下django集成使用xadmin后台的方法
2017/08/15 Python
详解如何利用Cython为Python代码加速
2018/01/27 Python
Pandas聚合运算和分组运算的实现示例
2019/10/17 Python
Pyqt5 关于流式布局和滚动条的综合使用示例代码
2020/03/24 Python
在脚本中单独使用django的ORM模型详解
2020/04/01 Python
Python实现查找数据库最接近的数据
2020/06/08 Python
python3爬虫GIL修改多线程实例讲解
2020/11/24 Python
Python 实现PS滤镜的旋涡特效
2020/12/03 Python
纯CSS3大转盘抽奖示例代码(响应式、可配置)
2017/01/13 HTML / CSS
利用HTML5画出一个坦克的形状具体实现代码
2013/06/20 HTML / CSS
小女主人连衣裙:Little Mistress
2017/07/10 全球购物
教师专业理论水平的自我评价分享
2013/11/09 职场文书
食品安全承诺书
2014/05/22 职场文书
教师党员公开承诺事项
2014/05/28 职场文书
2014年班组长工作总结
2014/11/20 职场文书
离婚被告答辩状
2015/05/22 职场文书
教师继续教育反思周记
2015/06/25 职场文书
初中物理教学反思
2016/02/19 职场文书
详解Apache SkyWalking 告警配置指南
2021/04/22 Servers