詳解pyenv下使用python matplotlib模塊的問(wèn)題解決
先來(lái)描述一下我遇到的問(wèn)題,在進(jìn)行matplotlib學(xué)習(xí)時(shí), plot.show() 總是無(wú)法成功運(yùn)行,總是會(huì)報(bào)一個(gè)錯(cuò):
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.
其實(shí)意思很簡(jiǎn)單,就是我用的python并不是一個(gè)作為系統(tǒng)框架存在的,因?yàn)槲覟榱朔奖愎芾韕ython的版本,選擇了 pyenv 這個(gè)管理工具,是一個(gè)獨(dú)立出來(lái)的python環(huán)境。
嘗試解決無(wú)果
參考網(wǎng)上眾多的解決方法,例如以下兩個(gè)最常見(jiàn)的:
方法一: 添加如下兩行 代碼解決:
>>> import matplotlib
>>> matplotlib.use('TkAgg')
##在import matplotlib下的模塊,如pyplot等之前添加上面2句
>>> import matplotlib.pyplot as plt
方法二: 添加一下matplotlib的配置:
echo "backend: TkAgg" >> ~/.matplotlib/matplotlibrc
然而,以上這兩種解決方式都***無(wú)法解決我的問(wèn)題***,此時(shí)出現(xiàn)了第二個(gè)錯(cuò)誤:
No module named '_tkinter'
說(shuō)是找不到 tkinter 這個(gè)模塊,找了網(wǎng)上大多數(shù)方法,全都是linux系統(tǒng)下的解決方案,我真的很好奇沒(méi)有一個(gè)使用mac的用戶出現(xiàn)我這樣的問(wèn)題嗎? 究其原因,是因?yàn)?,使?pyenv 獨(dú)立安裝出來(lái)的python中并沒(méi)有 tkinter 這個(gè)模塊,于是嘗試直接安裝 tkinter ,結(jié)果竟然提示沒(méi)有發(fā)現(xiàn) tkinter 包!
pip3 install tkinter Collecting tkinter Could not find a version that satisfies the requirement tkinter (from versions: ) No matching distribution found for tkinter
來(lái)到這,我不禁陷入了深深的思考,這個(gè) tkinter 到底是何方神圣,去了Python社區(qū):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.
說(shuō)白了, tkinter 就是一個(gè)利用python做GUI(圖形用戶界面),它提供各種標(biāo)準(zhǔn)的 GUI 接口項(xiàng),以利于迅速進(jìn)行高級(jí)應(yīng)用程序開(kāi)發(fā)。
那么究竟去哪安裝這個(gè) tkinter 包,說(shuō)實(shí)話到現(xiàn)在我也不知道如何利用 pyenv 去安裝 tkinter ,那這個(gè)問(wèn)題又該怎么解決呢?
曲線救國(guó)
既然 tkinter 這個(gè)GUI庫(kù)沒(méi)用,那換個(gè)庫(kù)是不是就好了呢?結(jié)果的確和我想的一樣,在我換了一個(gè)GUI庫(kù)之后,他的確成功了。 具體操作如下: 在出現(xiàn) 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. 這個(gè)錯(cuò)誤的時(shí)候,在終端輸入以下命令:
echo "backend : Qt5Agg" > ~/.matplotlib/matplotlibrc
如果提示你沒(méi)有安裝 PyQt 的話,你就需要執(zhí)行
brew install pyqt
然后在執(zhí)行
pip install PyQt5
這時(shí)候在運(yùn)行你的代碼就可以了。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
基于PyQt5制作Excel數(shù)據(jù)分組匯總器
這篇文章主要介紹了基于PyQt5制作的一個(gè)小工具:Excel數(shù)據(jù)分組匯總器。文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起試一試2022-01-01
使用Jupyter notebooks上傳文件夾或大量數(shù)據(jù)到服務(wù)器
這篇文章主要介紹了使用Jupyter notebooks上傳文件夾或大量數(shù)據(jù)到服務(wù)器,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-04-04
如何一鍵理清大型Python項(xiàng)目依賴樹(shù)
這篇文章主要介紹了如何一鍵理清大型Python項(xiàng)目依賴樹(shù),文章圍繞主題相關(guān)資料展開(kāi)詳細(xì)的內(nèi)容介紹,感興趣的小伙伴可以參考一下2022-06-06
PyCharm 創(chuàng)建指定版本的 Django(超詳圖解教程)
這篇文章主要介紹了PyCharm 創(chuàng)建指定版本的 Django,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-06-06
詳解Python自動(dòng)化之文件自動(dòng)化處理
今天給大家?guī)?lái)的是關(guān)于Python的相關(guān)知識(shí),文章圍繞著Python文件自動(dòng)化處理展開(kāi),文中有非常詳細(xì)的介紹及代碼示例,需要的朋友可以參考下2021-06-06
python+jinja2實(shí)現(xiàn)接口數(shù)據(jù)批量生成工具
這篇文章主要介紹了python+jinja2實(shí)現(xiàn)接口數(shù)據(jù)批量生成工具的操作方法,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-08-08
python模擬實(shí)現(xiàn)分發(fā)撲克牌
這篇文章主要為大家詳細(xì)介紹了python模擬實(shí)現(xiàn)分發(fā)撲克牌,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-04-04
Python?RawString與open文件的newline換行符遇坑解決
這篇文章主要為大家介紹了Python?RawString與open文件的newline換行符遇坑解決示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10
Python Opencv實(shí)現(xiàn)單目標(biāo)檢測(cè)的示例代碼
這篇文章主要介紹了Python Opencv實(shí)現(xiàn)單目標(biāo)檢測(cè)的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09

