詳解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的用戶(hù)出現(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(圖形用戶(hù)界面),它提供各種標(biāo)準(zhǔn)的 GUI 接口項(xiàng),以利于迅速進(jìn)行高級(jí)應(yīng)用程序開(kāi)發(fā)。
那么究竟去哪安裝這個(gè) tkinter 包,說(shuō)實(shí)話(huà)到現(xiàn)在我也不知道如何利用 pyenv 去安裝 tkinter ,那這個(gè)問(wèn)題又該怎么解決呢?
曲線(xià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 的話(huà),你就需要執(zhí)行
brew install pyqt
然后在執(zhí)行
pip install PyQt5
這時(shí)候在運(yùn)行你的代碼就可以了。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
python實(shí)現(xiàn)圖書(shū)館搶座(自動(dòng)預(yù)約)功能的示例代碼
這篇文章主要介紹了python實(shí)現(xiàn)圖書(shū)館搶座(自動(dòng)預(yù)約)功能,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09python實(shí)現(xiàn)整數(shù)的二進(jìn)制循環(huán)移位
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)整數(shù)的二進(jìn)制循環(huán)移位,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-03-03如何使用python的ctypes調(diào)用醫(yī)保中心的dll動(dòng)態(tài)庫(kù)下載醫(yī)保中心的賬單
這篇文章主要介紹了如何使用python的ctypes調(diào)用醫(yī)保中心的dll動(dòng)態(tài)庫(kù)下載醫(yī)保中心的賬單,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-05-05Python中集合類(lèi)型(set)學(xué)習(xí)小結(jié)
這篇文章主要介紹了Python中集合類(lèi)型(set)學(xué)習(xí)小結(jié),本文講解了set的初始化、運(yùn)算操作、基本方法等內(nèi)容,需要的朋友可以參考下2015-01-01python構(gòu)建自定義回調(diào)函數(shù)詳解
在工作中,回調(diào)函數(shù)使用的場(chǎng)景是非常多的,下面我們就來(lái)通過(guò)例子程序來(lái)詳細(xì)了解利用了Python的屬性機(jī)制構(gòu)建了一個(gè)自定義回調(diào)函數(shù)的使用2017-06-06python中requests使用代理proxies方法介紹
這篇文章主要介紹了python中requests使用代理proxies方法介紹,具有一定參考價(jià)值,需要的朋友可以了解下。2017-10-10