我创建了一个使用 Matplotlib/mathtext 字体的 wxapp,并使用 py2exe 将其转换为 exe。生成的 exe 在我的电脑上运行良好。但是,当我将它带到其他地方时,它会在 app.exe.log 中崩溃并显示以下内容:
~\dist\library.zip\matplotlib\font_manager.py:1216: UserWarning: findfont: Font family ['STIXGeneral'] not found. Falling back to Bitstream Vera Sans
~\dist\library.zip\matplotlib\font_manager.py:1226: UserWarning: findfont: Could not match :family=Bitstream Vera Sans:style=normal:variant=normal:weight=normal:stretch=normal:size=12. Returning c:\windows\fonts\browai.ttf
~\dist\library.zip\matplotlib\font_manager.py:1216: UserWarning: findfont: Font family ['STIXSizeOneSym'] not found. Falling back to Bitstream Vera Sans
~\dist\library.zip\matplotlib\font_manager.py:1226: UserWarning: findfont: Could not match :family=Bitstream Vera Sans:style=normal:variant=normal:weight=bold:stretch=normal:size=12. Returning c:\windows\fonts\browai.ttf
~\dist\library.zip\matplotlib\font_manager.py:1216: UserWarning: findfont: Font family ['STIXSizeThreeSym'] not found. Falling back to Bitstream Vera Sans
~\dist\library.zip\matplotlib\font_manager.py:1216: UserWarning: findfont: Font family ['STIXSizeFourSym'] not found. Falling back to Bitstream Vera Sans
~\dist\library.zip\matplotlib\font_manager.py:1216: UserWarning: findfont: Font family ['STIXSizeFiveSym'] not found. Falling back to Bitstream Vera Sans
~\dist\library.zip\matplotlib\font_manager.py:1216: UserWarning: findfont: Font family ['STIXSizeTwoSym'] not found. Falling back to Bitstream Vera Sans
~\dist\library.zip\matplotlib\font_manager.py:1226: UserWarning: findfont: Could not match :family=Bitstream Vera Sans:style=italic:variant=normal:weight=normal:stretch=normal:size=12. Returning c:\windows\fonts\browai.ttf
~\dist\library.zip\matplotlib\font_manager.py:1216: UserWarning: findfont: Font family ['STIXNonUnicode'] not found. Falling back to Bitstream Vera Sans
~\dist\library.zip\matplotlib\font_manager.py:1216: UserWarning: findfont: Font family ['cmb10'] not found. Falling back to Bitstream Vera Sans
~\dist\library.zip\matplotlib\font_manager.py:1216: UserWarning: findfont: Font family ['cmtt10'] not found. Falling back to Bitstream Vera Sans
~\dist\library.zip\matplotlib\font_manager.py:1216: UserWarning: findfont: Font family ['cmmi10'] not found. Falling back to Bitstream Vera Sans
~\dist\library.zip\matplotlib\font_manager.py:1216: UserWarning: findfont: Font family ['cmex10'] not found. Falling back to Bitstream Vera Sans
~\dist\library.zip\matplotlib\font_manager.py:1216: UserWarning: findfont: Font family ['cmsy10'] not found. Falling back to Bitstream Vera Sans
~\dist\library.zip\matplotlib\font_manager.py:1216: UserWarning: findfont: Font family ['cmr10'] not found. Falling back to Bitstream Vera Sans
~\dist\library.zip\matplotlib\font_manager.py:1216: UserWarning: findfont: Font family ['cmss10'] not found. Falling back to Bitstream Vera Sans
而且我还看到了一个以以下结尾的回溯:
...
File "matplotlib\mathtext.pyo", line 720, in _get_glyph
KeyError: 98
找不到的字体都是matplotlib/mathtext使用的。
我发现我尝试获取 data_files 的 setup.py 中的两种方法(使用 glob 和 matplotlib.get_py2exe_datafiles)都不起作用,并且没有任何内容被复制到我的 mpl-data/fonts 目录中。源码目录:C:\Python27\Lib\site-packages\matplotlib\mpl-data\fonts 包含 3 个文件夹:afm、pdfcorefonts & ttf。ttf 文件夹是上面提到的 mathtext 字体所在的位置。
在 setup.py 上运行 py2exe 并修改 glob 以获取 3 个字体目录后,我尝试手动将整个 mpl-data 文件夹复制到 dist 文件夹中,但仍然出现相同的错误。
似乎 matplotlib 的 font_manager 中的 findfont 方法在运行 py2exe 后不起作用,我猜当 mathtext.pyo 中的 _get_glyph 尝试使用 Bitstream Vera Sans 字体时它会崩溃。