MatplotlibDeprecationWarning with Pyinstaller .exe

11,863

Solution 1

Find here the pyinstaller issue that (partly) addresses this problem.

The deprecation warning has been introduced for matplotlib >=3.1. Accordingly, the environment variable MATPLOTLIBDATA will not be used anymore by pandas in future versions. However, PyInstaller currently relies on this variable for reasons not entirely clear to me.

The piece of code causing the warning is found in pyi_rth_mpldata.py:

os.environ["MATPLOTLIBDATA"] = os.path.join(sys._MEIPASS, "mpl-data")

Unfortunately, simply uncommenting the line locally (site-packages/PyInstaller/loader/rthooks/rpyi_rth_mpldata.py) is not an option, causing my PyInstaller bundles to crash.

I see currently the following options:

  1. downgrade matplotlib to v3.0 (pip install 'matplotlib==3.0.3')
  2. locally disable the deprecation warning in matplotlib (matplotlib/__init__.py:625)
  3. Filter warnings (see the other answer for how)
  4. wait until a patch is available, or help the pyinstaller crew in finding one...

Option 1. was working for me, hopefully you are lucky as well. Options 2. and 3. are the easiest and should not have any side effects.

Solution 2

If you just want to silence the warning: The deprecation warning inherets from UserWarning which inherits from Warning so you can filter it using filter_warnings() from the built-in warnings package. To do this, dump the following lines before any matplotlib imports.

import warnings
warnings.filterwarnings("ignore", "(?s).*MATPLOTLIBDATA.*", category=UserWarning)

The (?s) is the regex flag DOTALL which allows the .*s to match\ns contained within the warning's message.

You can test if it actually works outside of a PyInstaller build by running the following code after the above code.

import os, sys
# Artificially add the MATPLOTLIBDATA environment variable. This is reset
# when you restart your python console.
os.environ["MATPLOTLIBDATA"] = os.path.join(os.path.split(sys.executable)[0], "Lib/site-packages/matplotlib/mpl-data")

# Then proceed to load matplotlib
import matplotlib
Share:
11,863

Related videos on Youtube

blackbird
Author by

blackbird

Updated on June 23, 2022

Comments

  • blackbird
    blackbird almost 2 years

    I have run into a warning that only appears when the pyinstaller executable is run.

    ...appdata\local\programs\python\python37-32\lib\site-packages\PyInstaller\loader\pyimod03_importers.py:627: MatplotlibDeprecationWarning:
    The MATPLOTLIBDATA environment variable was deprecated in Matplotlib 3.1 and will be removed in 3.3.
      exec(bytecode, module.__dict__)
    

    I have tried all of the suggestions here: Python/matplotlib : getting rid of matplotlib.mpl warning

    I have also tried this without any change in the end result: Pyinstaller exe hide warning messages

    without any change in the MatplotlibDeprecation warnings appearing in the final executable. The warnings are baseline not present when running the code in an IDE such as Pycharm.

    Using: Python 3.7.2 Pyinstaller 3.5 Matplotlib 3.1.1