Search code examples
pythonmatplotlibjupyter-notebookanaconda

Error: while importing matplotlib on Jupyter notebook


I am trying to import matplotlib. I receive the below error when importing matplotlib on Jupyter Notebook.

I typed: import matplotlib

---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
Cell In[3], line 1
----> 1 import matplotlib

File C:\users\sarth\python\pandas\env\Lib\site-packages\matplotlib\__init__.py:161
    157 from packaging.version import parse as parse_version
    159 # cbook must import matplotlib only within function
    160 # definitions, so it is safe to import from it here.
--> 161 from . import _api, _version, cbook, _docstring, rcsetup
    162 from matplotlib.cbook import sanitize_sequence
    163 from matplotlib._api import MatplotlibDeprecationWarning

File C:\users\sarth\python\pandas\env\Lib\site-packages\matplotlib\rcsetup.py:27
     25 from matplotlib import _api, cbook
     26 from matplotlib.cbook import ls_mapper
---> 27 from matplotlib.colors import Colormap, is_color_like
     28 from matplotlib._fontconfig_pattern import parse_fontconfig_pattern
     29 from matplotlib._enums import JoinStyle, CapStyle

File C:\users\sarth\python\pandas\env\Lib\site-packages\matplotlib\colors.py:52
     49 from numbers import Real
     50 import re
---> 52 from PIL import Image
     53 from PIL.PngImagePlugin import PngInfo
     55 import matplotlib as mpl

File C:\users\sarth\python\pandas\env\Lib\site-packages\PIL\Image.py:88
     79 MAX_IMAGE_PIXELS: int | None = int(1024 * 1024 * 1024 // 4 // 3)
     82 try:
     83     # If the _imaging C module is not present, Pillow will not load.
     84     # Note that other modules should not refer to _imaging directly;
     85     # import Image and use the Image.core variable instead.
     86     # Also note that Image.core is not a publicly documented interface,
     87     # and should be considered private and subject to change.
---> 88     from . import _imaging as core
     90     if __version__ != getattr(core, "PILLOW_VERSION", None):
     91         msg = (
     92             "The _imaging extension was built for another version of Pillow or PIL:\n"
     93             f"Core version: {getattr(core, 'PILLOW_VERSION', None)}\n"
     94             f"Pillow version: {__version__}"
     95         )

ImportError: DLL load failed while importing _imaging: The specified module could not be found.

I have tried several ways including reinstalling python and creating a new environment on my system to no avail.

I am able to import other libraries such as pandas and numpy without any issues.


Solution

  • Try upgrading your version of pillow.

    pip install --upgrade Pillow
    

    or

    conda update Pillow
    

    Depending on your envoronment, this should work.