[Python] Pycharm uses the pyinstaller packaged exe to report an error ImportError: DLL load failed while importing _path: The specified module cannot be found.

Detailed error report

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

Find the line specified in matplotlib\transforms.py

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


Double-click to enter the file in Pycharm. The python_stubs file is an automatically generated file that contains virtual definitions of built-in functions
python_stubs\-1056256661\matplotlib.
This is used by PyCharm to infer the type of a builtin if it is not hardcoded for a given version.
According to the test, the file in python_stubs may be called by default when running pyinstaller in Pycharm, but it was not added when packaging.

solution

Delete python_stubs\-1056256661\matplotlibthe folder or package without Pycharm, directly console or vscode

PS1: Set Pyinstaller as an external tool in Pycharm

insert image description here
Pyinstaller Location: $ProjectFileDir$\venv\Scripts\pyinstaller.exe
Arguments: -F $FilePath$
Working Directory:$FileDir$

PS2: Use build.py to package the nicegui project in the virtual environment

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)
``

Guess you like

Origin blog.csdn.net/qq_25262697/article/details/129985305