【Python】Pycharm使用pyinstaller打包后的exe报错ImportError: DLL load failed while importing _path: 找不到指定的模块。

详细报错

File "main.py", line 12, in
File "PyInstaller\loader\pyimod02_importers.py", line 352, in exec_module
File "nicegui_init_.py", line 8, in
File "PyInstaller\loader\pyimod02_importers.py", line 352, in exec_module
File "nicegui\ui.py", line 81, in
File "PyInstaller\loader\pyimod02_importers.py", line 352, in exec_module
File "nicegui\elements\line_plot.py", line 3, in
File "PyInstaller\loader\pyimod02_importers.py", line 352, in exec_module
File "nicegui\elements\pyplot.py", line 4, in
File "PyInstaller\loader\pyimod02_importers.py", line 352, in exec_module
File "matplotlib_init_.py", line 161, in
File "PyInstaller\loader\pyimod02_importers.py", line 352, in exec_module
File "matplotlib\rcsetup.py", line 27, in
File "PyInstaller\loader\pyimod02_importers.py", line 352, in exec_module
File "matplotlib\colors.py", line 56, in
File "PyInstaller\loader\pyimod02_importers.py", line 352, in exec_module
File "matplotlib\scale.py", line 22, in
File "PyInstaller\loader\pyimod02_importers.py", line 352, in exec_module
File "matplotlib\ticker.py", line 138, in
File "PyInstaller\loader\pyimod02_importers.py", line 352, in exec_module
File "matplotlib\transforms.py", line 49, in
ImportError: DLL load failed while importing _path: 找不到指定的模块。
[19164] Failed to execute script 'main' due to unhandled exception!

Debug

找到matplotlib\transforms.py指定行

from matplotlib._path import (
affine_transform, count_bboxes_overlapping_bbox, update_path_extents)

在Pycharm中双击进入
文件位于
python_stubs\-1056256661\matplotlib
python_stubs文件是一个自动生成的文件,其中包含内置函数的虚拟定义。如果未针对给定版本对内置函数进行硬编码,则PyCharm使用它来推断内置函数的类型。
根据测试,在Pycharm中运行pyinstaller时可能默认调用了python_stubs中的文件,但是封装的时候没有加进来。

解决方案

删除python_stubs\-1056256661\matplotlib文件夹或者不用Pycharm打包,直接控制台或者vscode

PS1:Pycharm中设置Pyinstaller为外部工具

【Python】Pycharm使用pyinstaller打包后的exe报错ImportError: DLL load failed while importing _path: 找不到指定的模块。_第1张图片
Pyinstaller 位置:$ProjectFileDir$\venv\Scripts\pyinstaller.exe
实参:-F $FilePath$
工作目录:$FileDir$

PS2:虚拟环境中使用build.py打包nicegui项目

import os
import subprocess
from pathlib import Path
import nicegui

cmd = [
    'venv\\Scripts\\python.exe',
    '-m', 'PyInstaller',
    'main.py', # your main file with ui.run()
    '--name', 'myapp', # name of your app
    '--onefile',
    #'--windowed', # prevent console appearing, only use with ui.run(native=True, ...)
    '--add-data', f'{Path(nicegui.__file__).parent}{os.pathsep}nicegui'
]
subprocess.call(cmd)
``

你可能感兴趣的:(#,Python,#,Debug,python,pycharm,matplotlib)