Troubleshooting the import of `matplotlib` in Jupyter Notebooks

Greetings fellow Pythonistas!

I am taking a course on Python for data science on Udemy by an instructor named Colt Steele. It’s a great course. The tool used by Colt for teaching and completing practice exercises is Jupyter Notebooks. I am more than half the way through the material and everything has worked well up to this point. But now when I try to import matplotlib, Python is throwing an error involving Python libraries Pillow and libimagequant (which I gather are typically required libraries for handling and parsing images). Beyond that I am not sure what the traceback is trying to say or what changes I need to make for resolution.

Here is my problematic Python code snippet:

import pandas as pd
import matplotlib.pyplot as plt


billboard = pd.read_csv('data/billboard_charts.csv')
billboard.info()

Python/Jupyter doesn’t like that. Here is the output:

Import error
---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
Cell In[6], line 2
      1 import pandas as pd
----> 2 import matplotlib.pyplot as plt
      5 billboard = pd.read_csv('data/billboard_charts.csv')
      6 billboard.info()

File ~/anaconda3/lib/python3.8/site-packages/matplotlib/__init__.py:131
    127 from packaging.version import parse as parse_version
    129 # cbook must import matplotlib only within function
    130 # definitions, so it is safe to import from it here.
--> 131 from . import _api, _version, cbook, _docstring, rcsetup
    132 from matplotlib.cbook import sanitize_sequence
    133 from matplotlib._api import MatplotlibDeprecationWarning

File ~/anaconda3/lib/python3.8/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 ~/anaconda3/lib/python3.8/site-packages/matplotlib/colors.py:51
     49 from numbers import Number
     50 import re
---> 51 from PIL import Image
     52 from PIL.PngImagePlugin import PngInfo
     54 import matplotlib as mpl

File ~/.local/lib/python3.8/site-packages/PIL/Image.py:94
     85 MAX_IMAGE_PIXELS = int(1024 * 1024 * 1024 // 4 // 3)
     88 try:
     89     # If the _imaging C module is not present, Pillow will not load.
     90     # Note that other modules should not refer to _imaging directly;
     91     # import Image and use the Image.core variable instead.
     92     # Also note that Image.core is not a publicly documented interface,
     93     # and should be considered private and subject to change.
---> 94     from . import _imaging as core
     95     if __version__ != getattr(core, 'PILLOW_VERSION', None):
     96         raise ImportError("The _imaging extension was built for another "
     97                           "version of Pillow or PIL:\n"
     98                           "Core version: %s\n"
     99                           "Pillow version: %s" %
    100                           (getattr(core, 'PILLOW_VERSION', None),
    101                            __version__))

ImportError: libimagequant.so.0: cannot open shared object file: No such file or directory

When I comment out the line which says: import matplotlib.pyplot as plt from my code snippet quoted above, the import error goes away.

requirements.txt

aiohttp==3.9.3
aiosignal==1.3.1
annotated-types==0.6.0
anyascii==0.3.2
anyio==4.3.0
anytree==2.12.1
apparmor==3.1.7
application-utility==1.3.2
apsw==3.43.1.0
argcomplete==2.0.0
argon2-cffi==23.1.0
argon2-cffi-bindings==21.2.0
asciidoc==10.2.0
asttokens==2.4.1
async-lru==2.0.4
async-timeout==4.0.3
attrs==23.2.0
Authlib==1.2.1
autocommand==2.2.2
Babel==2.12.1
backcall==0.2.0
bcrypt==4.1.2
beautifulsoup4==4.12.2
bleach==6.1.0
Brotli==1.1.0
brotlicffi==1.1.0.0
btrfsutil==6.7.1
build==1.1.1
cachetools==5.3.2
cairocffi==1.6.1
certifi==2024.2.2
cffi==1.16.0
chardet==5.2.0
charset-normalizer==3.3.2
click==8.1.7
colorama==0.4.6
comm==0.2.1
configobj==5.0.8
contourpy==1.2.0
cryptography==41.0.7
css-parser==1.0.9
cssselect==1.2.0
cssselect2==0.7.0
cupshelpers==1.0
cycler==0.12.1
dbus-python==1.3.2
decorator==5.1.1
defusedxml==0.7.1
distro==1.9.0
dnspython==2.5.0
docopt==0.6.2
docutils==0.20.1
dropbox==11.36.2
duplicity==2.1.4
entrypoints==0.4
ewmh==0.1.6
executing==2.0.0
fasteners==0.19
fastjsonschema==2.19.1
faust-cchardet==2.1.19
feedparser==6.0.11
filelock==3.13.1
fonttools==4.49.0
frozenlist==1.4.1
future==0.18.3
Glances==3.4.0.3
google-api-core==2.15.0
google-api-python-client==2.111.0
google-auth==2.28.1
google-auth-httplib2==0.2.0
googleapis-common-protos==1.62.0
green-recorder==3.2.10
gufw==24.4.0
h11==0.14.0
html2text==2020.1.16
html5-parser==0.4.11
html5lib==1.1
httpcore==1.0.2
httplib2==0.22.0
httpx==0.26.0
idna==3.6
ifaddr==0.2.0
importlib_metadata==5.1.0
inflate64==1.0.0
inflect==7.0.0
iniconfig==2.0.0
installer==0.7.0
ipykernel==6.29.3
ipython==8.22.1
jaraco.context==4.3.0
jaraco.functools==4.0.0
jaraco.text==3.12.0
jedi==0.19.1
jeepney==0.8.0
Jinja2==3.1.3
json5==0.9.19
jsonschema==4.21.1
jsonschema-specifications==2023.12.1
jupyter-events==0.9.0
jupyter_client==8.6.0
jupyter_core==5.7.1
jupyter_server==2.13.0
jupyterlab==4.1.3
jupyterlab_pygments==0.3.0
jupyterlab_server==2.25.3
kiwisolver==1.4.5
lark==1.1.9
layoutswitcherlib==0.8.37
lensfun==0.3.4
Levenshtein==0.23.0
LibAppArmor==3.1.7
libtorrent==2.0.9
lightdm-gtk-greeter-settings==1.2.3
lit==16.0.6.dev0
lxml==4.9.2
Mako==1.3.2
mallard-ducktype==1.0.2
manjaro-sdk==0.1.1
Markdown==3.5.2
markdown-it-py==3.0.0
MarkupSafe==2.1.4
material-color-utilities-python==0.1.5
matplotlib==3.8.2
matplotlib-inline==0.1.6
mdurl==0.1.2
mechanize==0.4.8
meson==1.3.2
mistune==3.0.2
more-itertools==10.1.0
msgpack==1.0.5
multidict==6.0.4
multivolumefile==0.2.3
mutagen==1.47.0
nbclient==0.9.0
nbconvert==7.16.2
nbformat==5.9.2
nest_asyncio==1.5.9
netifaces==0.11.0
netsnmp-python==1.0a1
nftables==0.1
notebook==7.1.1
notebook_shim==0.2.4
npyscreen==4.10.5
numpy==1.26.4
oauth2client==4.1.3
ordered-set==4.1.0
overrides==7.7.0
packaging==23.2
pacman_mirrors==4.24.0
pandocfilters==1.5.1
paramiko==2.11.1
parso==0.8.3
pdftotext==2.2.2
pexpect==4.9.0
pia==3.4
pickleshare==0.7.5
pillow==10.2.0
pip-tools==7.4.1
pipx==1.4.3
platformdirs==4.1.0
pluggy==1.4.0
ply==3.11
poetry-core==1.9.0
prometheus_client==0.20.0
prompt-toolkit==3.0.43
protobuf==4.25.3
psutil==5.9.8
ptyprocess==0.7.0
pulsectl==23.5.2
pure-eval==0.2.2
pwquality==1.4.5
py7zr==0.21.0
pyaml==23.9.0
pyasn1==0.5.0
pyasn1_modules==0.3.0
pybcj==1.0.2
pycairo==1.26.0
pychm==0.8.6
pycparser==2.21
pycryptodome==3.20.0
pycryptodomex==3.12.0
pycups==2.0.1
pycurl==7.45.2
pydantic==2.6.3
pydantic_core==2.16.3
pydbus==0.6.0
PyDrive2==1.17.0
Pygments==2.17.2
PyGObject==3.46.0
pyinotify==0.9.6
PyNaCl==1.4.0
pyOpenSSL==24.0.0
pyparsing==3.1.1
pyppmd==1.1.0
pyproject_hooks==1.0.0
PyQt5==5.15.10
PyQt5-sip==12.13.0
PyQt6==6.6.1
PyQt6-sip==13.6.0
PyQt6-WebEngine==6.6.0
pyrsistent==0.19.3
PySide2==5.15.12
PySide6==6.6.2
pytest==7.4.4
python-dateutil==2.9.0
python-distutils-extra==2.39
python-json-logger==2.0.7
python-prctl==1.8.1
python-xlib==0.33
pytz==2024.1
pyxdg==0.28
PyYAML==6.0.1
pyzmq==25.1.2
pyzstd==0.15.9
ranger-fm==1.9.3
rapidfuzz==3.6.0
Recoll==1.37.4
recoll-aspell-python-py3==1.15
recollchm==0.8.4.1+git
referencing==0.33.0
regex==2023.12.25
reportlab==4.1.0
requests==2.31.0
requests-file==1.5.1
rich==13.7.1
rpds-py==0.17.1
rsa==4.9
ruamel.yaml==0.17.40
ruamel.yaml.clib==0.2.8
Send2Trash==1.8.2
setproctitle==1.3.3
setuptools-scm==8.0.4
sgmllib3k==1.0.0
shiboken2==5.15.12
shiboken6==6.6.2
shiboken6-generator==6.6.2
six==1.16.0
sniffio==1.3.1
soupsieve==2.5
stack-data==0.6.3
stone==3.3.1
svglib==1.5.1
systemd-python==235
TBB==0.2
testpath==0.6.0
texttable==1.7.0
tinycss2==1.2.1
tldextract==5.1.1
tomli==2.0.1
tornado==6.3.2
tqdm==4.66.2
traitlets==5.14.1
trove-classifiers==2024.3.3
typing_extensions==4.10.0
uc-micro-py==1.0.3
ueberzug==18.2.1
ufw==0.36.2
ujson==5.9.0
ulauncher==5.15.6
unrardll==0.1.7
uritemplate==4.1.1
urllib3==1.26.18
userpath==1.9.2
validate==5.0.8
validate-pyproject==0.13.post1.dev0+gb752273.d20230520
wcwidth==0.2.13
webencodings==0.5.1
websocket-client==1.7.0
wxPython==4.2.1
xxhash==3.4.1
Yapsy==1.12.2
yarl==1.9.4
zeroconf==0.63.0
zipp==3.17.0

Based on all the information above, what can you people tell me about the import error? What is Python trying to say in greater detail and what would you people recommend I try next? What other information could I provide to help troubleshoot?

Did you try installing the pillow and libimagequant libraries via pip install?

Hi Tacy. Yes:

With pip inside my venv.
(venv) $ pip install pillow
error: externally-managed-environment

× This environment is externally managed
╰─> To install Python packages system-wide, try 'pacman -S
    python-xyz', where xyz is the package you are trying to
    install.
    
    If you wish to install a non-Arch-packaged Python package,
    create a virtual environment using 'python -m venv path/to/venv'.
    Then use path/to/venv/bin/python and path/to/venv/bin/pip.
    
    If you wish to install a non-Arch packaged Python application,
    it may be easiest to use 'pipx install xyz', which will manage a
    virtual environment for you. Make sure you have python-pipx
    installed via pacman.

note: If you believe this is a mistake, please contact your Python installation or OS distribution provider. You can override this, at the risk of breaking your Python installation or OS, by passing --break-system-packages.
hint: See PEP 668 for the detailed specification.
And natively.
› yay -Sy libimagequant pillow
:: Synchronizing package databases...
 core is up to date
 extra is up to date
 community is up to date
 multilib is up to date
 -> No AUR package found for pillow
Sync Dependency (1): libimagequant-4.3.0-1
:: Synchronizing package databases...
 core is up to date
 extra is up to date
 community is up to date
 multilib is up to date
warning: libimagequant-4.3.0-1 is up to date -- reinstalling
resolving dependencies...
looking for conflicting packages...

Packages (1) libimagequant-4.3.0-1

Total Installed Size:  0.62 MiB
Net Upgrade Size:      0.00 MiB

:: Proceed with installation? [Y/n] 
(1/1) checking keys in keyring                     [######################] 100%
(1/1) checking package integrity                   [######################] 100%
(1/1) loading package files                        [######################] 100%
(1/1) checking for file conflicts                  [######################] 100%
(1/1) checking available disk space                [######################] 100%
:: Running pre-transaction hooks...
(1/1) Creating Timeshift snapshot before upgrade...
==> skipping timeshift-autosnap due skipRsyncAutosnap in /etc/timeshift-autosnap.conf set to TRUE.
:: Processing package changes...
(1/1) reinstalling libimagequant                   [######################] 100%
:: Running post-transaction hooks...
(1/1) Arming ConditionNeedsUpdate...

you are using conda, so you should be using conda install matplotlib (conda specifically disables pip here it seems)

Hi Cornelius. Thanks for your reply.

Here is:

matplotlib with conda
(venv) $ conda install matplotlib
Retrieving notices: ...working... done
Collecting package metadata (current_repodata.json): done
Solving environment: done

## Package Plan ##

  environment location: /home/gnull/anaconda3

  added / updated specs:
    - matplotlib


The following packages will be downloaded:

    package                    |            build
    ---------------------------|-----------------
    ca-certificates-2023.12.12 |       h06a4308_0         126 KB
    certifi-2024.2.2           |   py38h06a4308_0         159 KB
    ------------------------------------------------------------
                                           Total:         285 KB

The following packages will be UPDATED:

  ca-certificates    conda-forge::ca-certificates-2023.11.~ --> pkgs/main::ca-certificates-2023.12.12-h06a4308_0 
  certifi            conda-forge/noarch::certifi-2023.11.1~ --> pkgs/main/linux-64::certifi-2024.2.2-py38h06a4308_0 


Proceed ([y]/n)? y


Downloading and Extracting Packages
                                                                                
Preparing transaction: done                                                     
Verifying transaction: done
Executing transaction: done

It looks like it completed the installation of some package or packages. But I then returned to my Jupyter Notebook, I restarted the kernel, and I re-ran the cell. The same issue is present.

When I tried to install pillow with conda, it says already installed and when I tried installing libquant with conda, it says: “Not found”.

pillow and libimagequant with conda:
$ conda install pillow    
Collecting package metadata (current_repodata.json): done
Solving environment: done

# All requested packages already installed.

(venv) $ conda install libimagequant        
Collecting package metadata (current_repodata.json): done
Solving environment: failed with initial frozen solve. Retrying with flexible solve.
Collecting package metadata (repodata.json): done
Solving environment: failed with initial frozen solve. Retrying with flexible solve.

PackagesNotFoundError: The following packages are not available from current channels:

  - libimagequant

Current channels:

  - https://repo.anaconda.com/pkgs/main/linux-64
  - https://repo.anaconda.com/pkgs/main/noarch
  - https://repo.anaconda.com/pkgs/r/linux-64
  - https://repo.anaconda.com/pkgs/r/noarch

To search for alternate channels that may provide the conda package you're
looking for, navigate to

    https://anaconda.org

and use the search bar at the top of the page.

Is the jupyter notebook running from within the conda environment? This SO page might help.

I’m not sure what to make of that SO link. My reading of it points to how to start a Jupyter Notebook. My Jupyter Notebook is running successfully. I know this because I am able to otherwise create and run Python in other cells. If I comment out the problematic import matplotlib.pyplot as plt line and run the cell, padas is imported as pd and I can continue on with my coding session.

The SO link seems to show how to run Anaconda via CLI, if I understand it correctly. That isn’t an issue for me because I am able to launch and run using the Anaconda Navigator. See here:

I have no experience with anaconda navigator. The SO link is about how to make sure that your jupyter notebook is actually using of the virtual environment you install packages in.