Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ModuleNotFoundError if I import pandas before pyplot

I ran into a weird bug this morning that I'm hoping to get some background info on. I have a work around, but I'd like to know if there is some deeper issue. I could not find anything on SE or elsewhere online.

The bug: If I import pandas before matplotlib.pyplot I get the error message:

Traceback (most recent call last):
  File "test.py", line 5, in <module>
    import matplotlib.pyplot as plt
  File "/usr/lib64/python3.6/site-packages/matplotlib/pyplot.py", line 115, in <module>
    _backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup()
  File "/usr/lib64/python3.6/site-packages/matplotlib/backends/__init__.py", line 32, in pylab_setup
    globals(),locals(),[backend_name],0)
ModuleNotFoundError: No module named 'matplotlib.backends.backend_qt4agg'

Code that works:

import matplotlib as mpl
import matplotlib.pyplot as plt
import pandas

plt.plot([1,2,3],[3,2,1])
plt.show()

Code that doesn't work any more:

import pandas
import matplotlib as mpl
import matplotlib.pyplot as plt

plt.plot([1,2,3],[3,2,1])
plt.show()

Note: running the code from a file fails (python3 test.py), but running it from the command line interpreter works.

I attempted to update my python packages this morning, though I didn't think it did anything. I wanted to get Pandas 23 to use a function I saw in their docs. I updated pip3 ('pip install --user pip') thinking it would have the most recent version. I had some trouble with pip 10.0.1 (see https://github.com/pypa/pip/issues/5221 ), but that seems to have been magically resolved despite not really doing any of the proposed solutions on that thread. In any case, it turned out I was using the most recent version of Pandas available on pip (0.20.1), so I kept that. I also did a 'dnf update' this morning too trying to solve the pip issue.

I'm using Python 3.6.5, Pandas 0.20.1, and Matplotlib 2.0.0. Running Fedora 26. My code was working fine (with pandas importing before pyplot) until the attempted updates.

The point is, I did several things that could have broken it and I'd like to find out why. It seems wrong that the import order should matter. Any ideas on what is causing this? If it is actually a bug (and not just a pebkac issue), where do I report it?

Cheers

like image 579
sluginthemud Avatar asked May 18 '18 18:05

sluginthemud


1 Answers

The backend settings in ~/.matplotlib/matplotlibrc file. Change it from TkAgg to Agg or other backend, if you installed.

Or you can try this,

import matplotlib
matplotlib.use('Agg')
like image 123
E. Zeytinci Avatar answered Sep 25 '22 15:09

E. Zeytinci