Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matplotlib directory not found while using Pyinstaller to create exe from py files

I've been trying to create an exe file from my py files. There are multiple py files, however, 1 entry point file. My code takes input from html, csv, xml files and generate a word file as an output.

I'm using Python 3.9, tried using Pyinstaller 4.2, 5(dev). Both giving the same error. Conversion is successful if I try to convert a file without matplotlib in it. I've tried different versions of matplotlib also. Specifically, 4.3.1, 4.3.0rc1, 3.2.2. However, everytime I'm getting the same error.

assert mpl_data_dir, "Failed to determine matplotlib's data directory!"

AssertionError: Failed to determine matplotlib's data directory!

I've also tried to make changes to the hook files as well, according to similar problem faced by other people, however, still the same problem persists.

like image 613
Vikramaditya Avatar asked May 01 '21 10:05

Vikramaditya


People also ask

Why is the .EXE file created by PyInstaller not working?

The most common reason a PyInstaller package fails is that PyInstaller failed to bundle a required file. Such missing files fall into a few categories: Hidden or missing imports: Sometimes PyInstaller can't detect the import of a package or library, typically because it is imported dynamically.

Do you need Python installed to run PyInstaller EXE?

They do not need to have Python installed at all. The output of PyInstaller is specific to the active operating system and the active version of Python. This means that to prepare a distribution for: a different OS.

Why is EXEC_statement () not working in Matplotlib?

It's an error caused by the pyinstaller matplotlib hook, for some reason the exec_statement () function that is supposed to be getting the data path is not working. This is what worked for me: Go to the folder where pyinstaller is installed. Go to the hooks folder. Locate and open hook-matplotlib.py file.

What is pyinstaller and how to use it?

Pyinstaller is a python module using which the python file (.py) can be converted to an executable file (.exe). Install the pyinstaller directly on the default environment using the below command.

Can I change the hook-Matplotlib file in pyinstaller?

I tried changing the hook-matplotlib.py file as others suggested, but it turns out the newest version of Pyinstaller already has these changes, so as long as your pyinstaller is updated, you should be fine in that aspect (check by running pip install pyinstaller --upgrade)

How to convert Python to exe file?

In this tutorial, we will talk about converting the python to exe file and then creating a setup that can be installed. Pyinstaller is a python module using which the python file (.py) can be converted to an executable file (.exe). Install the pyinstaller directly on the default environment using the below command.


3 Answers

Thanks to wedesoft:

pip uninstall pathlib

did the job. I replaced pathlib code with os.path; everything works perfect.

NOTICE: Updating matplotlib or pyinstaller didn't help me. I'm sure it'll be fixed in the newer pyinstaller version. More about the same issue here:

https://github.com/pyinstaller/pyinstaller/issues/5004

like image 115
Mujeeb Ishaque Avatar answered Oct 23 '22 06:10

Mujeeb Ishaque


It's an error caused by the pyinstaller matplotlib hook, for some reason the exec_statement() function that is supposed to be getting the data path is not working. This is what worked for me:

  1. Go to the folder where pyinstaller is installed.
  2. Go to the hooks folder.
  3. Locate and open hook-matplotlib.py file.
  4. Delete the PyInstaller import and then import matplotlib.
  5. change the exec_statement() function to matplotlib.get_data_path() function, you can delete the assert.

If you followed correctly, your code should look like this:

import matplotlib

mpl_data_dir = matplotlib.get_data_path()
datas = [ 
    (mpl_data_dir, "matplotlib/mpl-data"), 
]
like image 4
Lucas Vinícius Avatar answered Oct 23 '22 07:10

Lucas Vinícius


I don't understand why, but the issue was solved when we installed matplotlib==3.0.2 and pyinstaller==4.2

like image 2
constructor Avatar answered Oct 23 '22 06:10

constructor