12

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.

2
  • pyinstaller is tricky to create exe and many times you need to package it. can you share the minimum code that can reproduce your error? then we can try to advise you. Commented May 1, 2021 at 14:16
  • I recommend using the Anaconda Distribution, it's likely to make your life much easier. Package List Commented Oct 14, 2021 at 18:25

7 Answers 7

8

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

Sign up to request clarification or add additional context in comments.

3 Comments

This solved the problem for me. I'm running python 3.8.10, matplotlib 3.4.2
thanks, i ran into the same problem and tried so many things, now found this and it just works ^^
Still had this problem in 2023 and uninstalling pathlib worked.
4

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"), 
]

4 Comments

Please don't add images of code. You can add code as text. Refer How do I format my posts using Markdown or HTML?
I didn't know about the visual editor, thanks!
I am not able to find the hooks folder where pyinstaller is located in my system.
This is the simpliest solution for me, you are a genius Lucas!. For this I created a fresh new conda enviroment with the ANACONDA NAVIGATOR, not the Anaconda Prompt. then I installed every library that my python program needs and pyinstaller. Then looked for the folder that contains the pyinstaller library, in my case C:\Users\MyWindows10\anaconda3\envs\ExeBirth\Lib\site-packages\PyInstaller(ExeBirth is just the name of my conda env), then edited hook-matplotlib.py file as Lucas said, and voilá, problem solved
2

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

2 Comments

so what if someone needs to include it?
matplotlib removed the default variable for the data directory, hence why it can't be found. However, it was only removed after matplotlib v3.2.2, hence why downgrading to anything below that would fix this issue and solve your issue
2

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

However, I was still running into the 'Failed to determine matplotlib's data directory!' issue and was able to resolve it by:

  1. Running pip uninstall pathlib

This is a weird one, but there's (unfortunately) two pathlib packages in the Python world: one built-in and a different one on PyPi. If you're using the PyPi one by chance, the get_data_path() function in hook-matplotlib.py fails to work as expected and will cause that assertion to fail

  1. In Pyinstaller command, include --hidden-import matplotlib

Not sure if this one is 100% necessary, but I included it in my pyinstaller command to make sure the matplotlib hook file was being used (Ex: pyinstaller --onefile --windowed --hidden-import "matplotlib" ...

Hope this is helpful, this resolution was really frustrating to figure out

2 Comments

I have to say I'm so lucky to come across your suggestion above. Saved me a great deal of time and frustration! Thanks.
Since I'm using anaconda, is this possible solution still legit?
0

You can stick to the current matplotlib version but have to update pyinstaller (5.0.dev0):

pip install -U https://github.com/pyinstaller/pyinstaller/archive/develop.zip

1 Comment

This seemed to overcome this error for me.
0

The error in my case (Python 3.8, PyInstaller 4.3, matplotlib 3.3.3) was this:

 ........
 File "c:\users\dev\appdata\local\programs\python\python38\lib\ntpath.py", line 293, in expanduser
    path = os.fspath(path)
TypeError: expected str, bytes or os.PathLike object, not WindowsPath
Traceback (most recent call last):
    ........
    assert mpl_data_dir, "Failed to determine matplotlib's data directory!"

I had a dependency on pathlib which broke the PyInstaller hook for matplotlib. After removing pathlib from setup.py/requirements.txt and uninstalling pathlib it worked (pathlib is part of Python 3.8, so no need to install the old module).

Comments

0

I ran into this when trying to create an bare executable after I uninstalled matplotlib and PyInstaller was trying to pack the non-existent module.

The pip uninstall didn't fully delete matplotlib's directory from site-packages, after deleting it manually (same for PyQt5), the exe was built as expected.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.