详解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开发之for循环操作实例详解
Nov 12 Python
Python简单生成随机数的方法示例
Mar 31 Python
Python基于最小二乘法实现曲线拟合示例
Jun 14 Python
python实现时间o(1)的最小栈的实例代码
Jul 23 Python
python批量获取html内body内容的实例
Jan 02 Python
使用selenium模拟登录解决滑块验证问题的实现
May 10 Python
python面向对象 反射原理解析
Aug 12 Python
Python生成器next方法和send方法区别详解
May 30 Python
Python lambda表达式原理及用法解析
Aug 18 Python
在pycharm中使用pipenv创建虚拟环境和安装django的详细教程
Nov 30 Python
pandas将list数据拆分成行或列的实现
Dec 13 Python
python获取淘宝服务器时间的代码示例
Apr 22 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 4.2书写安全的脚本
2006/10/09 PHP
POSIX 风格和兼容 Perl 风格两种正则表达式主要函数的类比(preg_match, preg_replace, ereg, ereg_replace)
2010/10/12 PHP
php+jQuery递归调用POST循环请求示例
2016/10/14 PHP
对于Laravel 5.5核心架构的深入理解
2018/02/22 PHP
在IE6下发生Internet Explorer cannot open the Internet site错误
2010/06/21 Javascript
70+漂亮且极具亲和力的导航菜单设计国外网站推荐
2011/09/20 Javascript
最简单的js图片切换效果实现代码
2011/09/24 Javascript
js实现广告漂浮效果的小例子
2013/07/02 Javascript
JS检测输入字符是否包含非法字符的示例代码
2014/02/11 Javascript
Jquery Ajax方法传值到action的方法
2014/05/11 Javascript
一个奇葩的最短的 IE 版本判断JS脚本
2014/05/28 Javascript
javascript组合使用构造函数模式和原型模式实例
2015/06/04 Javascript
深入浅析JSON.parse()、JSON.stringify()和eval()的作用详解
2016/04/03 Javascript
如何利用Promises编写更优雅的JavaScript代码
2016/05/17 Javascript
全面了解javascript三元运算符
2016/06/27 Javascript
利用JS对iframe父子(内外)页面进行操作的方法教程
2017/06/15 Javascript
JS运动特效之任意值添加运动的方法分析
2018/01/24 Javascript
vue-router判断页面未登录自动跳转到登录页的方法示例
2018/11/04 Javascript
详解如何在Javascript和Sass之间共享变量
2019/11/13 Javascript
压缩Vue.js打包后的体积方法总结(Vue.js打包后体积过大问题)
2020/02/03 Javascript
vue实现滚动鼠标滚轮切换页面
2020/12/13 Vue.js
基于Python对象引用、可变性和垃圾回收详解
2017/08/21 Python
Python实现的矩阵转置与矩阵相乘运算示例
2019/03/26 Python
十行代码使用Python写一个USB病毒
2019/06/21 Python
python 多线程死锁问题的解决方案
2020/08/25 Python
Pytest单元测试框架如何实现参数化
2020/09/05 Python
CSS的background属性及CSS3的背景图片设置总结
2016/06/13 HTML / CSS
中国领先的专业演出票务网:永乐票务
2016/08/29 全球购物
碧欧泉美国官网:Biotherm美国
2016/08/31 全球购物
Hanro官网:奢华男士和女士内衣、睡衣和家居服
2018/10/25 全球购物
马来西亚在线药房:RoyalePharma
2019/12/01 全球购物
酒店端午节活动方案
2014/08/26 职场文书
个人工作作风整改措施思想汇报
2014/10/13 职场文书
团拜会主持词
2015/07/04 职场文书
Redis keys命令的具体使用
2022/06/05 Redis
win10频率超出范围怎么办?win10老显示超出工作频率范围的解决方法
2022/07/07 数码科技