TypeError: expected str, bytes or os.PathLike object, not WindowsPath while converting .py using Pyinstaller

TypeError: expected str, bytes or os.PathLike object, not WindowsPath while converting .py using Pyinstaller

尝试使用 Pyinstaller 构建 .exe 时,抛出此错误:

    133235 INFO: Loading module hook 'hook-matplotlib.backends.py' from 'c:\users\jimit vaghela\appdata\local\programs\python\python37\lib\site-packages\PyInstaller\hooks'...
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "c:\users\jimit vaghela\appdata\local\programs\python\python37\lib\site-packages\matplotlib\__init__.py", line 901, in <module>
    fail_on_error=True)
  File "c:\users\jimit vaghela\appdata\local\programs\python\python37\lib\site-packages\matplotlib\__init__.py", line 796, in _rc_params_in_file
    with _open_file_or_url(fname) as fd:
  File "c:\users\jimit vaghela\appdata\local\programs\python\python37\lib\contextlib.py", line 112, in __enter__
    return next(self.gen)
  File "c:\users\jimit vaghela\appdata\local\programs\python\python37\lib\site-packages\matplotlib\__init__.py", line 770, in _open_file_or_url
    fname = os.path.expanduser(fname)
  File "c:\users\jimit vaghela\appdata\local\programs\python\python37\lib\ntpath.py", line 291, in expanduser
    path = os.fspath(path)
TypeError: expected str, bytes or os.PathLike object, not WindowsPath
134074 INFO: Loading module hook 'hook-matplotlib.py' from 'c:\users\jimit vaghela\appdata\local\programs\python\python37\lib\site-packages\PyInstaller\hooks'...
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "c:\users\jimit vaghela\appdata\local\programs\python\python37\lib\site-packages\matplotlib\__init__.py", line 901, in <module>
    fail_on_error=True)
  File "c:\users\jimit vaghela\appdata\local\programs\python\python37\lib\site-packages\matplotlib\__init__.py", line 796, in _rc_params_in_file
    with _open_file_or_url(fname) as fd:
  File "c:\users\jimit vaghela\appdata\local\programs\python\python37\lib\contextlib.py", line 112, in __enter__
    return next(self.gen)
  File "c:\users\jimit vaghela\appdata\local\programs\python\python37\lib\site-packages\matplotlib\__init__.py", line 770, in _open_file_or_url
    fname = os.path.expanduser(fname)
  File "c:\users\jimit vaghela\appdata\local\programs\python\python37\lib\ntpath.py", line 291, in expanduser
    path = os.fspath(path)
TypeError: expected str, bytes or os.PathLike object, not WindowsPath

我在 Whosebug 上找到了一个解决方案,其中指出需要将代码插入到 Pyinstaller 文件夹的 backend.py 中。但这也不管用。

这里出了什么问题?

所以,我发现 matplotlib 是问题所在。在 Pyinstaller 的模块参数中排除它修复了它。

像这样:

pyinstaller --exclude-module matplotlib main.py

目前 matplotlib 存在一个问题。

我通过编辑源文件解决了这个问题。如果你不想编辑源文件,据说可以安装低版本的matplotlib比如3.0.3来避免这个问题。

我的情况是行不通。无论如何,以下是我采取的步骤。

首先打开Python解释器并复制你得到的路径作为输出。

>>> import matplotlib
>>> matplotlib.get_data_path() # copy the below path
'C:\<Python Path>\lib\site-packages\matplotlib\mpl-data'

接下来,打开文件 C:\<Python Path>\lib\site-packages\PyInstaller\hooks\hook-matplotlib.py。为了安全起见,如果需要,请备份此文件

from PyInstaller.utils.hooks import exec_statement

# old line; delete this
mpl_data_dir = exec_statement(
    "import matplotlib; print(matplotlib.get_data_path())")

# Add this line
mpl_data_dir = 'C:\<Python Path>\lib\site-packages\matplotlib\mpl-data'

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

datas = [
    (mpl_data_dir, "matplotlib/mpl-data"),
]

别忘了保存。