简体   繁体   中英

When I try to run “import matplotlib.pyplot as plt”, I receive the following error: “ModuleNotFoundError: No module named 'PIL'”

EDIT: I noticed that this problem only occurs when I'm using Jupyter Notebook. When I tried to use Google Colab, it worked fine. Is there a way to resolve this issue?

I tried uninstalling and installing pillow, image, and even matplotlib, but I keep receiving the same error. Note that when I try to run "import image", "import pil" and "import matplotlib", they all worked fine.

This is the exact message:

ModuleNotFoundError Traceback (most recent call last)

in

  5 import matplotlib.pyplot as plt

~\Anaconda3\lib\site-packages\matplotlib\pyplot.py in

 34 from cycler import cycler
 35 import matplotlib
 36 import matplotlib.colorbar
 37 import matplotlib.image
 38 from matplotlib import rcsetup, style

~\Anaconda3\lib\site-packages\matplotlib\colorbar.py in

 42 import matplotlib.collections as collections
 43 import matplotlib.colors as colors
 44 import matplotlib.contour as contour
 45 import matplotlib.cm as cm
 46 import matplotlib.gridspec as gridspec

~\Anaconda3\lib\site-packages\matplotlib\contour.py in

 15 import matplotlib.collections as mcoll
 16 import matplotlib.font_manager as font_manager
 17 import matplotlib.text as text
 18 import matplotlib.cbook as cbook
 19 import matplotlib.mathtext as mathtext

~\Anaconda3\lib\site-packages\matplotlib\text.py in

 14 from .font_manager import FontProperties
 15 from .patches import FancyArrowPatch, FancyBboxPatch, Rectangle
 16 from .textpath import TextPath  # Unused, but imported by others.
 17 from .transforms import (
 18     Affine2D, Bbox, BboxBase, BboxTransformTo, IdentityTransform, Transform)

~\Anaconda3\lib\site-packages\matplotlib\textpath.py in

  9 from matplotlib.font_manager import FontProperties, get_font
 10 from matplotlib.ft2font import LOAD_NO_HINTING, LOAD_TARGET_LIGHT
 11 from matplotlib.mathtext import MathTextParser
 12 from matplotlib.path import Path
 13 from matplotlib.transforms import Affine2D

~\Anaconda3\lib\site-packages\matplotlib\mathtext.py in

 25 
 26 import numpy as np
 27 from PIL import Image
 28 from pyparsing import (
 29     Combine, Empty, FollowedBy, Forward, Group, Literal, oneOf, OneOrMore,

ModuleNotFoundError: No module named 'PIL'

Try importing like this

import PIL as Image

It's import PIL not import pil . Normally you should also be able to see which line causes the error. There is nothing wrong with the matplotlib import that you mentioned.

  1. First, install PIL through command line python -m pip install pillow .

  2. Second, use it, do in your script from PIL import Image .

I found that this issue only occurred when using Jupyter Notebook, so after several tries I uninstalled Anaconda and reinstalled it and now it works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM