Cannot import name '_png' from 'matplotlib'

Viewed 27995

I want to use matplotlib instead kivy_garden.graph. Actually, I tried this code to check if it works for me. I've had some problems with installing matplotlib but I have successfully(or not) done that.

When I started the code I got from matplotlib import _png ImportError: cannot import name '_png' from 'matplotlib' (D:\PyCharmProjects\kivyApp\venv\lib\site-packages\matplotlib\__init__.py) I reinstalled matplotlib and pip, tried another version of matplotlib and I don't know why it is not working for me. I have Python 3.7.5, pip 20.2.4 and matplotlib 3.3.3

7 Answers

Reverting to matplotlib version 3.0.2 didn't work for me, but with 3.1.3 it did.

python -m pip uninstall matplotlib
pip install matplotlib==3.1.3

Python 3.8.2

I was having this problem in Google Colab and couldn't solve it. The simple solution that I found was to install the stable version that is pip install -U matplotlib and restarted the runtime and it worked.

It's work now. I executed py -m pip uninstall matplotlib and after py -m pip install matplotlib --version=3.0.2 from terminal in PyCharm. The same commands in cmd and git bash didn't work.

If you are using a linux distribution the problem is in the installation of matplotlib.

uninstall the current version: pip uninstall matplotlib

The correct installation can be found at: https://matplotlib.org/stable/users/installing.html

Choose the correct code for your distribution from below:

Linux package manager If you are using the Python version that comes with your Linux distribution, you can install Matplotlib via your package manager, e.g.:

Debian / Ubuntu: sudo apt-get install python3-matplotlib

Fedora: sudo dnf install python3-matplotlib

Red Hat: sudo yum install python3-matplotlib

Arch: sudo pacman -S python-matplotlib

after this it should work fine.

You also can coment line where this import is :) (for me it is C:\Users\{username}\.kivy\garden\garden.matplotlib\backend_kivy.py , line 256) But i dont know what problems it can bring in the future.

Since this bug remains for so many years I gave up waiting for the kivy-garden community to fix it. So when deploying my kivy-based software, I also distribute the following quick-and-dirty fix. It finds the file and comments out that line automatically.

from pathlib import Path
# Join user's home directory with Garden's file path
file = Path.home().joinpath('.kivy/garden/garden.matplotlib/backend_kivy.py')
# Read file
lines = []
with open(file, 'r') as f:
    for line in f:
        lines.append(line)
# Write file
with open(file, 'w') as f:
    for line in lines:
        if line.startswith('from matplotlib import _png'):
            # comment out this troublesome line
            line = '#' + line
        f.write(line)

I'm also facing this issue in Google Colab

try this pip install matplotlib==3.1.3 work for me in Google Colab

Related