Python 导入错误:没有名为 backend_tkagg 的模块
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20582384/
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
ImportError: No module named backend_tkagg
提问by smith
I have such imports and code:
我有这样的进口和代码:
import pandas as pd
import numpy as np
import statsmodels.formula.api as sm
import matplotlib.pyplot as plt
#Read the data from pydatasets repo using Pandas
url = './file.csv'
white_side = pd.read_csv(url)
#Fitting the model
model = sm.ols(formula='budget ~ article_size',
data=white_side,
subset=white_side['producer'] == "Peter Hymanson")
fitted = model.fit()
print fitted.summary()
After execution of this code I have such errors:
执行这段代码后,我有这样的错误:
/usr/bin/python2.7 /home/seth/PycharmProjects/osiris_project/PMN_way/start.py
Traceback (most recent call last):
File "/home/seth/PycharmProjects/osiris_project/PMN_way/start.py", line 5, in <module>
import matplotlib.pyplot as plt
File "/usr/lib64/python2.7/site-packages/matplotlib/pyplot.py", line 98, in <module>
_backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup()
File "/usr/lib64/python2.7/site-packages/matplotlib/backends/__init__.py", line 25, in pylab_setup
globals(),locals(),[backend_name])
ImportError: No module named backend_tkagg
Process finished with exit code 1
I`m using openSUSE and pycharm community edition latest version with installed pandas, numpy, etc How can i fix this problem?
我正在使用 openSUSE 和 pycharm 社区版最新版本,并安装了 Pandas、numpy 等我该如何解决这个问题?
采纳答案by Luke Woodward
I've seen this before, also on openSUSE (12.3). The fix is to edit the default matplotlibrc file.
我以前在 openSUSE (12.3) 上也见过这个。修复方法是编辑默认的 matplotlibrc 文件。
Here's how you find where the default matplotlibrc file lives, and where it lives on my machine:
以下是如何找到默认 matplotlibrc 文件所在的位置以及它在我的机器上的位置:
>>> import matplotlib
>>> matplotlib.matplotlib_fname()
'/usr/lib64/python2.7/site-packages/matplotlib/mpl-data/matplotlibrc'
The backend setting is the first configuration option in this file. Change it from TkAggto Agg, or to some other backend you have installed on your system. The comments in the matplotlibrc file list all backends supported by matplotlib.
后端设置是此文件中的第一个配置选项。将其从 更改TkAgg为Agg,或更改为您在系统上安装的其他后端。matplotlibrc 文件中的注释列出了 matplotlib 支持的所有后端。
The backend specified in this file is only the default; you can still change it at runtime by adding the following two lines, before any other matplotlib import:
这个文件中指定的后端只是默认的;您仍然可以在运行时通过在任何其他 matplotlib 导入之前添加以下两行来更改它:
import matplotlib
matplotlib.use("Agg") # or whichever backend you wish to use
回答by Sepp
I use openSuse 13.1 and had the same error "ImportError: No module named backend_tkagg".
我使用 openSuse 13.1 并出现相同的错误“ImportError: No module named backend_tkagg”。
I solved it by using this suggestion: http://forums.opensuse.org/showthread.php/416182-Python-matplolib.
我通过使用这个建议解决了这个问题:http: //forums.opensuse.org/showthread.php/416182-Python-matplolib。
I've installed the python-matplotlib-tk package, and now it is working just fine.
我已经安装了 python-matplotlib-tk 包,现在它工作得很好。
E.g. you can use: zypper install python-matplotlib-tk
例如,您可以使用: zypper install python-matplotlib-tk
回答by user3731622
I was able to fix this by putting
我能够通过放置来解决这个问题
import matplotlib.backends.backend_tkagg
above
以上
import matplotlib.pyplot as plt
Note, I received the same error while trying to run an executable generated using Py2exe.
请注意,我在尝试运行使用Py2exe.
Here is what I got when I ran TheProgram.exe from the command prompt:
这是我从命令提示符运行 TheProgram.exe 时得到的结果:
>>TheProgram.exe
Traceback (most recent call last):
File "ThePythonScriptToMakeIntoExe.py", line 14, in <module>
File "C:\Python34\lib\site-packages\matplotlib\pyplot.py", line 109, in <module>
_backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup()
File "C:\Python34\lib\site-packages\matplotlib\backends\__init__.py", line 32, in pylab_setup
globals(),locals(),[backend_name],0)
ImportError: No module named 'matplotlib.backends.backend_tkagg'
回答by Yuchao Jiang
I tried various solutions, only this works for me:
我尝试了各种解决方案,只有这对我有用:
sudo pip install matplotlib --upgrade

