TypeError:在使用Pyinstaller转换.py时,应为str、bytes或os.PathLike对象,而不是WindowsPath

2024-06-06 18:18:47 发布

您现在位置:Python中文网/ 问答频道 /正文

尝试使用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

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

这里出了什么问题


Tags: inpymatplotliblibpackageslocallinesite
2条回答

这是目前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"),
]

别忘了存钱

因此,我发现matplotlib是问题所在。排除Pyinstaller中模块参数中的该选项后,修复了该问题

像这样:

pyinstaller exclude-module matplotlib main.py

相关问题 更多 >