matplotlib:运行时错误:Python 未作为框架安装
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34977388/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
matplotlib: RuntimeError: Python is not installed as a framework
提问by KDD
This question has been asked before, in here, also here. However, the solution didn't fix the problem for my case.
这个问题之前已经问过,在这里,也在这里。但是,该解决方案并没有解决我的案例的问题。
The original error is, when I try to import matplotlib.pyplot
, I got:
最初的错误是,当我尝试时import matplotlib.pyplot
,我得到:
Traceback (most recent call last): File "", line 1, in File "/Users/XX/anaconda/lib/python2.7/site-packages/matplotlib/pyplot.py", line 114, in _backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup() File "/Users/XX/anaconda/lib/python2.7/site-packages/matplotlib/backends/init.py", line 32, in pylab_setup globals(),locals(),[backend_name],0) File "/Users/XX/anaconda/lib/python2.7/site-packages/matplotlib/backends/backend_macosx.py", line 24, in from matplotlib.backends import _macosx 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 Working with Matplotlib in a virtual enviroment see 'Working with Matplotlib in Virtual environments' in the Matplotlib FAQ
回溯(最近一次调用):文件“”,第 1 行,在文件“/Users/XX/anaconda/lib/python2.7/site-packages/matplotlib/pyplot.py”中,第 114 行,在 _backend_mod、new_figure_manager 中, draw_if_interactive,_show = pylab_setup()文件“/Users/XX/anaconda/lib/python2.7/site-packages/matplotlib/backends/ INIT.py”,第 32 行,在 pylab_setup globals(),locals(),[backend_name],0) 文件“/Users/XX/anaconda/lib/python2.7/site-packages/matplotlib/backends/backend_macosx.py” , line 24, in from matplotlib.backends import _macosx RuntimeError: Python is not installed as a framework. 如果 Python 没有作为框架安装,Mac OS X 后端将无法正常运行。有关更多信息,请参阅 Python 文档在 Mac OS X 上安装 Python 作为框架。请重新安装 Python 作为框架,或尝试其他后端之一。如果您在虚拟环境中使用 Matplotlib,请参阅 Matplotlib 常见问题中的“在虚拟环境中使用 Matplotlib”
I followed the solutions to add a ~/.matplotlib/matplotlibrc
file with the code: backend: TkAgg
. After doing that, my error changed to:
我按照解决方案添加了一个~/.matplotlib/matplotlibrc
带有代码的文件:backend: TkAgg
. 这样做之后,我的错误变为:
/Users/XX/anaconda/lib/python2.7/site-packages/matplotlib/font_manager.py:273: UserWarning: Matplotlib is building the font cache using fc-list. This may take a moment. warnings.warn('Matplotlib is building the font cache using fc-list. This may take a moment.') objc[25120]: Class TKApplication is implemented in both /Users/XX/anaconda/lib/libtk8.5.dylib and /System/Library/Frameworks/Tk.framework/Versions/8.5/Tk. One of the two will be used. Which one is undefined. objc[25120]: Class TKMenu is implemented in both /Users/XX/anaconda/lib/libtk8.5.dylib and /System/Library/Frameworks/Tk.framework/Versions/8.5/Tk. One of the two will be used. Which one is undefined. objc[25120]: Class TKContentView is implemented in both /Users/XX/anaconda/lib/libtk8.5.dylib and /System/Library/Frameworks/Tk.framework/Versions/8.5/Tk. One of the two will be used. Which one is undefined. objc[25120]: Class TKWindow is implemented in both /Users/XX/anaconda/lib/libtk8.5.dylib and /System/Library/Frameworks/Tk.framework/Versions/8.5/Tk. One of the two will be used. Which one is undefined.
/Users/XX/anaconda/lib/python2.7/site-packages/matplotlib/font_manager.py:273: UserWarning: Matplotlib 正在使用 fc-list 构建字体缓存。这可能需要一点时间。warnings.warn('Matplotlib 正在使用 fc-list 构建字体缓存。这可能需要一点时间。') objc[25120]:类 TKApplication 在 /Users/XX/anaconda/lib/libtk8.5.dylib 和/System/Library/Frameworks/Tk.framework/Versions/8.5/Tk. 将使用两者之一。哪个是不确定的。objc[25120]:TKMenu 类在 /Users/XX/anaconda/lib/libtk8.5.dylib 和 /System/Library/Frameworks/Tk.framework/Versions/8.5/Tk 中实现。将使用两者之一。哪个是不确定的。objc[25120]:类 TKContentView 在 /Users/XX/anaconda/lib/libtk8.5.dylib 和 /System/Library/Frameworks/Tk.framework/Versions/8 中实现。5/吨。将使用两者之一。哪个是不确定的。objc[25120]:类 TKWindow 在 /Users/XX/anaconda/lib/libtk8.5.dylib 和 /System/Library/Frameworks/Tk.framework/Versions/8.5/Tk 中实现。将使用两者之一。哪个是不确定的。
I have no idea how to fix that. I'm not using a virtual machine. Could you help me? Thank you!
我不知道如何解决这个问题。我没有使用虚拟机。你可以帮帮我吗?谢谢!
PS: I found out that by adding:
PS:我发现通过添加:
import matplotlib
matplotlib.use('TkAgg')
导入 matplotlib
matplotlib.use('TkAgg')
before import matplotlib.pyplot
, it seems to work. But adding those two lines of codes every time is annoying... Does anyone know what's going on and how I can fix it? Thank you!
之前import matplotlib.pyplot
,它似乎有效。但是每次都添加这两行代码很烦人......有谁知道发生了什么以及我如何解决它?谢谢!
采纳答案by Joney
I run my script in virtualenv. Python version is 3.5.
我在 virtualenv 中运行我的脚本。Python 版本是 3.5。
Add a line:
添加一行:
backend: TkAgg
in file:
在文件中:
~/.matplotlib/matplotlibrc
This solved the problem.
这解决了问题。
If you want to know more about why adding this solves the problem, you can read about customizing matplotlib's backend. And TkAggsolves this issue because of it's dependency with Tkinter.
如果您想了解更多关于为什么添加它可以解决问题的信息,您可以阅读自定义 matplotlib 的后端。而TkAgg解决,因为它与Tkinter的依赖这个问题。
回答by Sibish
Below worked for me:
以下为我工作:
import matplotlib
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt
Reference: https://github.com/tensorflow/tensorflow/issues/2375
回答by Abin Jo Abraham
I was also having the same error. What I have done is to install miniconda packages and using the pythonw. Python app was already installed in my mac. Installation is as simple as breaking an egg. Just bash the .sh file in the terminal.
我也有同样的错误。我所做的是安装 miniconda 包并使用 pythonw。Python 应用程序已安装在我的 Mac 中。安装就像打破鸡蛋一样简单。只需在终端中 bash .sh 文件。
回答by hakan
I installed Jupyter Notebook in virtualenv and below worked for me:
我在 virtualenv 中安装了 Jupyter Notebook,以下对我有用:
At Console:
在控制台:
echo "backend: TkAgg" >> ~/.matplotlib/matplotlibrc
pip install matplotlib==2.1.0
On Notebook:
在笔记本上:
import matplotlib
Restart the kernel:
重启内核:
dataframe.plot.hist()
...
In my opinion, it will fix the error.
在我看来,它将修复错误。
回答by Massimiliano Caranzano
I had the same issues in python 2.7 in virtualenv and I managed to fix those by simply downgrading matplotlib to version 2.1.0
我在 virtualenv 中的 python 2.7 中遇到了同样的问题,我设法通过将 matplotlib 降级到 2.1.0 版来解决这些问题
回答by haruishi
I was using pyenv
so the matplotlibrc
path wasn't in the home directory, so I created this script to figure the path out and change the backend
to Tkagg
:
我正在使用,pyenv
所以matplotlibrc
路径不在主目录中,所以我创建了这个脚本来找出路径并将其更改backend
为Tkagg
:
vim $(python -c "import os,matplotlib; print(os.path.join(os.path.dirname(matplotlib.__file__), 'mpl-data/matplotlibrc'));")
This script should work with any python on your system though.
不过,此脚本应该适用于您系统上的任何 python。