详解pyenv下使用python matplotlib模块的问题解决
作者:YueYong 发布时间:2023-08-08 20:25:01
先来描述一下我遇到的问题,在进行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
这时候在运行你的代码就可以了。
来源:https://juejin.im/post/5bfd271951882528c4468165


猜你喜欢
- 抛开数据库,生活中的方言是什么?方言就是某个地方的特色语言,是一种区别于其它地方的语言,只有你们这一小块地方能听懂,出了这个地方又是另一种方
- msxml3.dll 错误 '80004005'未指定的错误/Project/lijiang_071017/include/
- 分页应该是在我们开发web应用时经常要做的工作,能够比较简洁的实现数据库和视图层的分页十分重要。在数据库层利用hibernate进行数据库的
- JavaScript中对象的property有三个属性:1.writable。该property是否可写。2.enumerable。当使用f
- 本文实例为大家分享了Vue实现简单跑马灯特效的具体代码,供大家参考,具体内容如下效果:点击按钮让文字动起来,点击停止按钮让文字停止知识点:s
- 一、简介实现计算机视觉任务的过程中,不可避免地需要对图像进行读写操作以及图像预处理操作,下面介绍两个常用的Python图像处理库:OpenC
- 本文转自微信公众号:"算法与编程之美"1、前言侧滑是一个非常实用的选项组件,它在Android App应用中非常广泛,常
- 什么是CSS Sprites?“Sprite”(精灵)这个词在计算机图形学中有它独特的定义,由于游戏、视频等画质越来越高,必须有一种技术可以
- 目录1. 安装方法2. 简单示例3. 兼容字典的所有操作4. 设置返回默认值5. 工厂函数自动创建key6. 序列化的支持字典是 Pytho
- 对于一些快速迭代的产品来说,特别是移动端 C端产品,基于用户运营的目的,在 app首页给用户展示各种各样的弹窗是很常见的事情,在产品初期,由
- DROP TABLE IF EXISTS [TEMP_TABLE_NAME]; create temporary table [TEMP_T
- var a= new Array(new Array(1,2),new Array('b','c')); d
- sys.argv[]是用来获取命令行参数的,sys.argv[0]表示代码本身文件路径,所以参数从1开始,以下两个例子说明:1、使用sys.
- 任务说明:编写一个钱币定位系统,其不仅能够检测出输入图像中各个钱币的边缘,同时,还能给出各个钱币的圆心坐标与半径。效果代码实现Canny边缘
- ->基础环境Linux:ubuntu 16.04Python ; 2.7->修改hostname1:$sudo hostname
- 点乘和矩阵乘的区别: 1)点乘(即“ * ”) ---- 各个矩阵对应元素做乘法若 w 为 m*1 的矩阵,x
- 描述:使用QtDesignner设计界面,pyQt5+python3实现主体方法制作的猜数字游戏。游戏规则:先选择游戏等级:初级、中级、高级
- 最近在老家找工作,无奈老家工作真心太少,也没什么面试机会,不过之前面试一家公司,提了一个有意思的需求,检测河面没有有什么船只之类的物体,我当
- 一、python魔法方法Python的魔法方法会在特定的情况下自动调用,且他们的方法名通常被双下划线包裹,之前我们学习的构造函数和析构函数就
- 在Python项目中,我们通常需要使用许多第三方库来提供额外的功能和工具。但是,直接将这些库上传到Git仓库并不是一种好的做法,因为这会使得