温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

怎么在pyenv中使用matplotlib模块

发布时间:2021-03-17 17:02:54 来源:亿速云 阅读:178 作者:Leah 栏目:开发技术

这篇文章将为大家详细讲解有关怎么在pyenv中使用matplotlib模块,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。

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

>>> 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中使用matplotlib模块

关于怎么在pyenv中使用matplotlib模块就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI