【问题标题】:MatplotlibDeprecationWarning with Pyinstaller .exeMatplotlibDeprecationWarning 与 Pyinstaller .exe
【发布时间】:2019-12-22 08:23:20
【问题描述】:

我遇到了仅在运行 pyinstaller 可执行文件时出现的警告。

...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__)

我已经尝试了这里的所有建议:Python/matplotlib : getting rid of matplotlib.mpl warning

我也试过这个,最终结果没有任何改变: Pyinstaller exe hide warning messages

最终可执行文件中出现的 MatplotlibDeprecation 警告没有任何变化。在 Pycharm 等 IDE 中运行代码时,警告是基线不存在的。

使用: Python 3.7.2 安装程序 3.5 Matplotlib 3.1.1

【问题讨论】:

标签: python-3.x matplotlib pyinstaller


【解决方案1】:

找到(部分)解决此问题的here the pyinstaller issue

已为 matplotlib >=3.1 引入了弃用警告。因此,在未来的版本中,pandas 将不再使用环境变量MATPLOTLIBDATA。但是,PyInstaller 目前依赖这个变量,原因我并不完全清楚。

导致警告的代码在pyi_rth_mpldata.py:

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

不幸的是,不能简单地在本地取消注释该行 (site-packages/PyInstaller/loader/rthooks/rpyi_rth_mpldata.py),这会导致我的 PyInstaller 包崩溃。

我目前看到以下选项:

  1. 将 matplotlib 降级到 v3.0 (pip install 'matplotlib==3.0.3')
  2. 在matplotlib中本地禁用弃用警告 (matplotlib/__init__.py:625)
  3. 过滤警告(请参阅other answer 了解如何)
  4. 等到补丁可用,或者帮助 pyinstaller 团队找到一个...

选项 1. 对我有用,希望你也很幸运。选项 2. 和 3. 是最简单的,应该没有任何副作用。

【讨论】:

    【解决方案2】:

    如果您只想消除警告:弃用警告继承自 UserWarning,它继承自 Warning,因此您可以使用内置警告包中的 filter_warnings() 对其进行过滤。为此,请在任何 matplotlib 导入之前转储以下行。

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

    (?s) 是正则表达式标志 DOTALL,它允许 .*s 匹配警告消息中包含的\ns。

    您可以通过在上述代码之后运行以下代码来测试它是否真的在 PyInstaller 构建之外工作。

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-05-17
      • 2022-10-23
      • 2022-12-12
      • 2016-06-07
      • 1970-01-01
      • 1970-01-01
      • 2013-08-22
      • 2020-12-17
      相关资源
      最近更新 更多