Importerror: Cannot Import Name '_Docstring' from Partially Initialized Module 'Matplotlib' (Most Likely Due to a Circular Import)

I am trying to import matplotlib in .ipynb file but its not working, none of my files have the same name as _docstring yet I get this error and if I try something in .py file, it works fine.

import numpy as np 
import matplotlib.pyplot as plt
import cv2 as cv

I'm running this in a .ipynb file in VS Code

Output :

ImportError                               Traceback (most recent call last)
Cell In[3], line 2
      1 import numpy as np 
----> 2 import matplotlib.pyplot as plt
      3 import cv2 as cv

File c:\Users\P****\AppData\Local\Programs\Python\Python38\lib\site-packages\matplotlib\__init__.py:113
    109 from packaging.version import parse as parse_version
    111 # cbook must import matplotlib only within function
    112 # definitions, so it is safe to import from it here.
--> 113 from . import _api, _version, cbook, _docstring, rcsetup
    114 from matplotlib.cbook import sanitize_sequence
    115 from matplotlib._api import MatplotlibDeprecationWarning

ImportError: cannot import name '_docstring' from partially initialized module 'matplotlib' (most likely due to a circular import) (c:\Users\P****\AppData\Local\Programs\Python\Python38\lib\site-packages\matplotlib\__init__.py)

But if I try to import matplotlib in .py file like

import matplotlib.pyplot as plt
import numpy as np

x = np.arange(1,10,0.1)
y = x**2

plt.plot(x,y)
plt.show()

It runs fine, no issues.

1

2 Answers

Reinstalling the matplotlib package resolved the issue.

pip install --upgrade --force-reinstall matplotlib

1

Try deleting matplotlib folder and reinstalling it.

Full path: AppData\Roaming\Python\Python39\site-packages\matplotlib

It worked for me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Elena Rostova

Elena Rostova

Lead Health, Wellness & Medical Journalist

Elena Rostova holds a Master's degree in Public Health Journalism. She covers groundbreaking medical research, holistic wellness trends, mental health awareness, and nutritional science.

Share this article
Twitter Facebook Pinterest