ImportError: No module named backend_tkagg

Viewed 33579

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?

8 Answers

I faced this issue before, I solved it by using an old version of matplotlib which is version 3.0.0 if you don't have this version try pip install matplotlib==3.0.0 hope this was helpful!

I faced this issue while doing live plotting using the 2.2.2 version. Tried the following:

pip install matplotlib==3.0.0 --user

It's still an older version but it worked.

I add my answer for those who might encounter this problem using CARLA Simulator.

Using Python 3.6, and installing matplotlib v 2.2.5 solved this problem for me.

To remove matplotlib and install the right version you can run

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.

If you are missing the requirements.txt file, here you are

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.

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. This is because different Python versions might not be compatible with the current pip version you are using. If it is too late for you and you already messed up with your terminal you can visit This tutorial 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.

I solved the problem by setting the variable before starting python

export MPLBACKEND=Agg; python3

Taken from here.

Related