简体   繁体   English

导入错误:没有名为 backend_tkagg 的模块

[英]ImportError: No module named backend_tkagg

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 Jackson")
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 等我该如何解决这个问题?

I've seen this before, also on openSUSE (12.3).我以前见过这个,也在 openSUSE (12.3) 上。 The fix is to edit the default matplotlibrc file.修复方法是编辑默认的 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 TkAgg to Agg , or to some other backend you have installed on your system.将其从TkAgg更改为Agg ,或更改为您已安装在系统上的其他后端。 The comments in the matplotlibrc file list all backends supported by matplotlib. 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

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 包,现在它工作得很好。

Eg you can use: zypper install python-matplotlib-tk例如,您可以使用: zypper install python-matplotlib-tk

我尝试了各种解决方案,只有这对我有用:

sudo pip install matplotlib --upgrade

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'

I faced this issue while doing live plotting using the 2.2.2 version.我在使用2.2.2版本进行实时绘图时遇到了这个问题。 Tried the following:尝试了以下方法:

pip install matplotlib==3.0.0 --user

It's still an older version but it worked.它仍然是一个旧版本,但它有效。

I solved the problem by setting the variable before starting python我在启动python之前通过设置变量解决了这个问题

export MPLBACKEND=Agg; python3

Taken from here .取自这里

我之前遇到过这个问题,我通过使用旧版本的 matplotlib 解决了它,如果你没有这个版本,请尝试pip install matplotlib==3.0.0希望这对你有帮助!

I add my answer for those who might encounter this problem using CARLA Simulator.我为那些使用 CARLA Simulator 可能遇到此问题的人添加了我的答案。

Using Python 3.6, and installing matplotlib v 2.2.5 solved this problem for me.使用 Python 3.6 并安装 matplotlib v 2.2.5 为我解决了这个问题。

To remove matplotlib and install the right version you can run要删除 matplotlib 并安装正确的版本,您可以运行

pip uninstall -r requirements.txt

Then add matplotlib==2.2.5 to the requirement file and run pip install -r requirements.txt --user to install the correct versions.然后将matplotlib==2.2.5添加到需求文件并运行pip install -r requirements.txt --user以安装正确的版本。

If you are missing the requirements.txt file, here you are如果您丢失了 requirements.txt 文件,请点击此处

Pillow>=3.1.2
numpy>=1.14.5
protobuf>=3.6.0
pygame>=1.9.4
matplotlib==2.2.5
future>=0.16.0
scipy>=0.17.0

In case you have different python versions and you would like to use a specific python version using py -<version> will use the right python version.如果您有不同的 python 版本,并且您想使用特定的 python 版本, py -<version>将使用正确的 python 版本。

WARNING: Changing Path variable of the current Python version might be dangerous and specially for linux users it might cause that you can not open the terminal again.警告:更改当前 Python 版本的 Path 变量可能很危险,特别是对于 linux 用户,它可能会导致您无法再次打开终端。 This is because different Python versions might not be compatible with the current pip version you are using.这是因为不同的 Python 版本可能与您使用的当前 pip 版本不兼容。 If it is too late for you and you already messed up with your terminal you can visit Thistutorial to fix your issue.如果对您来说太晚了并且您已经弄乱了您的终端,您可以访问本教程来解决您的问题。

After all of the fixes you might still get this error:完成所有修复后,您可能仍会收到此错误:

TypeError: blit() got an unexpected keyword argument 'colormode'

So you might want to edit the live_plotter.py file and remove colormode attribute from all the lines which are passing it to blit() method.因此,您可能想要编辑live_plotter.py文件并从将其传递给blit()方法的所有行中删除colormode属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM