Repository: pyinstaller/pyinstaller

Issue:
Impossible to build with matplotlib 3.3 on Python 3.7 and 3.8
## Description of the issue

Trying a simple program main.py
```python
import matplotlib.pyplot as plt

plt.plot([0,1,2], [0,1,1])
```

Running the application created with pyinstaller --hiddenimport='pkg_resources.py2_warn'  main.py

I get:

```
Traceback (most recent call last):                                                                                                                                                                                                                                                                                             File "main.py", line 3, in <module>                                                                                                                                                                                                                                                                                          File "<frozen importlib._bootstrap>", line 983, in _find_and_load                                                                                                                                                                                                                                                            File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked                                                                                                                                                                                                                                                   File "<frozen importlib._bootstrap>", line 677, in _load_unlocked                                                                                                                                                                                                                                                            File "c:\users\***\appdata\local\programs\python\python37\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 623, in exec_module                                                                                                                                                                              File "site-packages\matplotlib\__init__.py", line 898, in <module>                                                                                                                                                                                                                                                           File "site-packages\matplotlib\cbook\__init__.py", line 480, in _get_data_path                                                                                                                                                                                                                                               File "site-packages\matplotlib\__init__.py", line 239, in wrapper                                                                                                                                                                                                                                                            File "site-packages\matplotlib\__init__.py", line 534, in get_data_path                                                                                                                                                                                                                                                      File "site-packages\matplotlib\__init__.py", line 239, in wrapper                                                                                                                                                                                                                                                            File "site-packages\matplotlib\__init__.py", line 563, in _get_data_path                                                                                                                                                                                                                                                   NameError: name 'defaultParams' is not defined                                                                                                                                                                                                                                                                               [3064] Failed to execute script main
```
## Context information (for bug reports)

* Output of `pyinstaller --version`: ```3.6 and 4.0-dev0```
* Version of Python: 3.7 and 3.8
* Platform: Windows (english 10)
* Did you also try this on another platform? No

## What solves it

Reverting to matplotlib 3.2.2 resolve the issue (temporarily).


Discussion:
Also happens on Fedora 32 with matplotlib 3.3:
```
(venv) [rok@alpha main]$ ./main 
/home/rok/tmp/mpl/venv/lib64/python3.8/site-packages/PyInstaller/loader/pyimod03_importers.py:493: MatplotlibDeprecationWarning: Matplotlib installs where the data is not in the mpl-data subdirectory of the package are deprecated since 3.2 and support for them will be removed two minor releases later.
  exec(bytecode, module.__dict__)
Traceback (most recent call last):
  File "main.py", line 1, 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 "/home/rok/tmp/mpl/venv/lib64/python3.8/site-packages/PyInstaller/loader/pyimod03_importers.py", line 493, in exec_module
    exec(bytecode, module.__dict__)
  File "matplotlib/__init__.py", line 898, in <module>
  File "matplotlib/cbook/__init__.py", line 480, in _get_data_path
  File "matplotlib/__init__.py", line 239, in wrapper
  File "matplotlib/__init__.py", line 534, in get_data_path
  File "matplotlib/__init__.py", line 239, in wrapper
  File "matplotlib/__init__.py", line 563, in _get_data_path
NameError: name 'defaultParams' is not defined
[16899] Failed to execute script main
```
As per the initial warning (`MatplotlibDeprecationWarning: Matplotlib installs where the data is not in the mpl-data subdirectory of the package are deprecated since 3.2 and support for them will be removed two minor releases later.`), matplotlib 3.3.0 requires the `mpl-data` directory to be in `dist/matplotlib/` instead of `dist/` itself.

The issue can be temporarily fixed by manually moving the `mpl-data` directory into `matplotlib` directory.
Well for starters: `matplotlib.plot` doesn't exist. Use the following as a minimal example.
```
import matplotlib.pyplot as plt

plt.plot([0,1,2], [0,1,1])
plt.show()
```
But yes, it looks like @rokm is right. It's the data folder and that deprecation warning we've been putting off fixing has finally bit us. In which case the hook needs updating. I'll work on this.



Yeah sorry I wrote it from memory but I used pyplot.plt of course.

I ended up being able to make all of it work by reverting to a older matplotlib… I'll keep that for now as it reduces the amount of manual steps for distributing. And I'll update once the new hook is out.

Thanks for taking the time to work on the hook!

Resolving patch:
diff --git a/PyInstaller/hooks/hook-matplotlib.py b/PyInstaller/hooks/hook-matplotlib.py
--- a/PyInstaller/hooks/hook-matplotlib.py
+++ b/PyInstaller/hooks/hook-matplotlib.py
@@ -16,5 +16,5 @@
     "import matplotlib; print(matplotlib._get_data_path())")
 
 datas = [
-    (mpl_data_dir, "mpl-data"),
+    (mpl_data_dir, "matplotlib/mpl-data"),
 ]
diff --git a/PyInstaller/hooks/rthooks/pyi_rth_mpldata.py b/PyInstaller/hooks/rthooks/pyi_rth_mpldata.py
deleted file mode 100644
--- a/PyInstaller/hooks/rthooks/pyi_rth_mpldata.py
+++ /dev/null
@@ -1,16 +0,0 @@
-#-----------------------------------------------------------------------------
-# Copyright (c) 2013-2020, PyInstaller Development Team.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-#
-# The full license is in the file COPYING.txt, distributed with this software.
-#
-# SPDX-License-Identifier: Apache-2.0
-#-----------------------------------------------------------------------------
-
-
-import os
-import sys
-
-os.environ["MATPLOTLIBDATA"] = os.path.join(sys._MEIPASS, "mpl-data")


Test patch:
diff --git a/tests/functional/test_hooks/test_matplotlib.py b/tests/functional/test_hooks/test_matplotlib.py
--- a/tests/functional/test_hooks/test_matplotlib.py
+++ b/tests/functional/test_hooks/test_matplotlib.py
@@ -98,13 +98,6 @@ def test_matplotlib(pyi_builder, monkeypatch,
     if not configdir.startswith(tempfile.gettempdir()):
         raise SystemExit('MPLCONFIGDIR not pointing to temp directory.')
 
-    # Matplotlib's data directory should point to sys._MEIPASS.
-    # This is deprecated in matplotlib 3.1.0 and will be removed in 3.3.0
-    datadir = os.environ['MATPLOTLIBDATA']
-    print('MATPLOTLIBDATA:', repr(datadir))
-    if not datadir.startswith(sys._MEIPASS):
-        raise SystemExit('MATPLOTLIBDATA not pointing to sys._MEIPASS.')
-
     # Test access to the standard 'mpl_toolkits' namespace package installed
     # with Matplotlib. Note that this import was reported to fail under
     # Matplotlib 1.3.0.