简体   繁体   中英

Error after using PyInstaller for script with Matplotlib

In my code I am using:

import matplotlib.pyplot as plt
import matplotlib.image as mpimg

When I build the .py script, everything works fine. But after using the pyinstaller.exe to get the executable file, I get following error:

Traceback (most recent call last):
  File "C:\Program Files\Python38\Scripts\excel_to_python.py", line 10, in <module>
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "c:\program files\Python38\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 493, in exec_module
  File "matplotlib\__init__.py", line 913, in <module>
  File "matplotlib\__init__.py", line 812, in _rc_params_in_file
  File "contextlib.py", line 113, in __enter__
  File "matplotlib\__init__.py", line 790, in _open_file_or_url
FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\usr\\AppData\\Local\\Temp\\_MEI640402\\matplotlib\\mpl-data\\matplotlibrc'
[62044] Failed to execute script mpl_script

I tried to reinstall the matplotlib and pyinstaller but it is all the same.

  1. Install the Visual C++ Redistributable Package (vc_redist.x64.exe or vc_redist.x86.exe depending on your Windows). You can find it here: the latest supported visual c downloads , here: vc_redist.x64 or here: vc_redist.x86 , then
  2. rebuild your executable with pyinstaller.

Analysis

I had the same error in Windows 7 64-bit with Python 3.8.7:

Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.
C:\Users\w\Desktop\1\dist>MyTool.exe
Traceback (most recent call last):
  File "MyTool.py", line 9, in <module>
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "PyInstaller\loader\pyimod03_importers.py", line 531, in exec_module
  File "matplotlib\__init__.py", line 913, in <module>
  File "matplotlib\__init__.py", line 812, in _rc_params_in_file
  File "contextlib.py", line 113, in __enter__
  File "matplotlib\__init__.py", line 790, in _open_file_or_url
FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\w\\AppData\\Local\\Temp\\_MEI17002\\matplotlib\\mpl-data\\matplotlibrc'
[916] Failed to execute script MyTool

I rebuilt the executable with debug level as per pyinstaller-python :

pyinstaller --onefile --log-level=DEBUG MyTool.py

In the building traces I saw the matplotlib Traceback below:

18885 INFO: Loading module hook 'hook-matplotlib.backends.py' from 'c:\\users\\w\\appdata\\local\\programs\\python\\python38\\lib\\site-packages\\PyInstaller\\hooks'...
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "c:\users\w\appdata\local\programs\python\python38\lib\site-packages\matplotlib\__init__.py", line 174, in <module>
    _check_versions()
  File "c:\users\w\appdata\local\programs\python\python38\lib\site-packages\matplotlib\__init__.py", line 159, in _check_versions
    from . import ft2font
ImportError: DLL load failed while importing ft2font: The specified module could not be found.
19108 INFO: Loading module hook 'hook-matplotlib.py' from 'c:\\users\\w\\appdata\\local\\programs\\python\\python38\\lib\\site-packages\\PyInstaller\\hooks'...
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "c:\users\w\appdata\local\programs\python\python38\lib\site-packages\matplotlib\__init__.py", line 174, in <module>
    _check_versions()
  File "c:\users\w\appdata\local\programs\python\python38\lib\site-packages\matplotlib\__init__.py", line 159, in _check_versions
    from . import ft2font
ImportError: DLL load failed while importing ft2font: The specified module could not be found.

That led me to: dll load failed while importing ft2font , matplotlib cant load ft2font and matplotlib import ft2font . There were several answers there which suggested installing Visual C++ Redistributable Package, and that was actually the only thing that helped.

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