Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cannot import name 'ft2font' from 'matplotlib' on windows10

I am using a windows10 machine and using PyCharm as my IDE, so when I tried to run the below code, it shows the following error

I tried uninstalling and re-installing matplotlib and also as someone from SO suggested used --force reinstall too, but didn't work

It was working well until yesterday

import matplotlib.pyplot as plt
import numpy as np

 rng = np.random.RandomState(42)
 X = 10 * rng.rand(50)
 y = 2 * X - 1 + rng.randn(50)

plt.scatter(X, y)
plt.show()

Error showing on both Pycharm and Vs Code

Traceback (most recent call last):
File "D:/PyCharm/exercise/numpy_exercise/pandas_py.py", line 1, in 
<module>
import matplotlib.pyplot as plt
File "C:\Users\smile\AppData\Roaming\Python\Python37\site- 
packages\matplotlib\__init__.py", line 143, in <module>
from matplotlib import ft2font
ImportError: cannot import name 'ft2font' from 'matplotlib' 
 (C:\Users\smile\AppData\Roaming\Python\Python37\site- 
 packages\matplotlib\__init__.py)

 Error showing on Jupyter Notebook

<ipython-input-1-1842680e3d86> in <module>
  1 import pandas as pd
  2 import numpy as np
    ----> 3 import matplotlib.pyplot as plt
  4 get_ipython().run_line_magic('matplotlib', 'inline')
  5 

 ~\AppData\Roaming\Python\Python37\site-packages\matplotlib\pyplot.py in 
 <module>
 30 from cycler import cycler
 31 import matplotlib
   ---> 32 import matplotlib.colorbar
 33 import matplotlib.image
 34 from matplotlib import rcsetup, style

 ~\AppData\Roaming\Python\Python37\site-packages\matplotlib\colorbar.py 
 in <module>
 25 
 26 import matplotlib as mpl
 ---> 27 import matplotlib.artist as martist
 28 import matplotlib.cbook as cbook
 29 import matplotlib.collections as collections

 ModuleNotFoundError: No module named 'matplotlib.artist'
like image 573
arun Avatar asked Aug 31 '19 06:08

arun


1 Answers

I had a similar problem which was solved simply by forcing the reinstall of matplotlib:

python -m pip install -I matplotlib
  • The python -m pip instead of pip install to make sure the correct python executable is used
  • The -I forces reinstall
like image 161
np8 Avatar answered Sep 17 '22 03:09

np8