[Python] PyInstaller PyQt5 hook error - failed to import module

Discussão em 'Python' iniciado por Stack, Dezembro 21, 2024.

  1. Stack

    Stack Membro Participativo

    I'm trying to package a project using pyinstaller. It fails to create .exe. I have been going through the imports one by one and it seems that it is the Pandas import which fails because of a hook for pyqt5:

    File "C:\ProgramData\Anaconda3\envs\clonebasic\lib\site-packages\PyInstaller\hooks\hook-PyQt5.py", line 12, in from PyInstaller.utils.hooks.qt import get_qt_binaries, pyqt5_library_info ImportError: cannot import name 'get_qt_binaries' from 'PyInstaller.utils.hooks.qt' (C:\ProgramData\Anaconda3\envs\clonebasic\lib\site-packages\PyInstaller\utils\hooks\qt_init_.py)

    During handling of the above exception, another exception occurred:

    Traceback (most recent call last): File "C:\ProgramData\Anaconda3\envs\clonebasic\Scripts\pyinstaller-script.py", line 10, in sys.exit(run()) File "C:\ProgramData\Anaconda3\envs\clonebasic\lib\site-packages\PyInstaller_main_.py", line 179, in run run_build(pyi_config, spec_file, **vars(args)) File "C:\ProgramData\Anaconda3\envs\clonebasic\lib\site-packages\PyInstaller_main_.py", line 60, in run_build PyInstaller.building.build_main.main(pyi_config, spec_file, **kwargs) File "C:\ProgramData\Anaconda3\envs\clonebasic\lib\site-packages\PyInstaller\building\build_main.py", line 963, in main build(specfile, distpath, workpath, clean_build) File "C:\ProgramData\Anaconda3\envs\clonebasic\lib\site-packages\PyInstaller\building\build_main.py", line 885, in build exec(code, spec_namespace) File "H:\report_analysis\report_analysis\main.spec", line 7, in a = Analysis( File "C:\ProgramData\Anaconda3\envs\clonebasic\lib\site-packages\PyInstaller\building\build_main.py", line 410, in init self.postinit() File "C:\ProgramData\Anaconda3\envs\clonebasic\lib\site-packages\PyInstaller\building\datastruct.py", line 173, in postinit self.assemble() File "C:\ProgramData\Anaconda3\envs\clonebasic\lib\site-packages\PyInstaller\building\build_main.py", line 579, in assemble self.graph.process_post_graph_hooks(self) File "C:\ProgramData\Anaconda3\envs\clonebasic\lib\site-packages\PyInstaller\depend\analysis.py", line 329, in process_post_graph_hooks module_hook.post_graph(analysis) File "C:\ProgramData\Anaconda3\envs\clonebasic\lib\site-packages\PyInstaller\depend\imphook.py", line 447, in post_graph self._load_hook_module(keep_module_ref=True) File "C:\ProgramData\Anaconda3\envs\clonebasic\lib\site-packages\PyInstaller\depend\imphook.py", line 390, in _load_hook_module raise ImportErrorWhenRunningHook(self.hook_module_name, self.hook_filename) PyInstaller.exceptions.ImportErrorWhenRunningHook: Failed to import module __PyInstaller_hooks_0_PyQt5 required by hook for module C:\ProgramData\Anaconda3\envs\clonebasic\lib\site-packages\PyInstaller\hooks\hook-PyQt5.py. Please check whether module __PyInstaller_hooks_0_PyQt5 actually exists and whether the hook is compatible with your version of C:\ProgramData\Anaconda3\envs\clonebasic\lib\site-packages\PyInstaller\hooks\hook-PyQt5.py: You might want to read more about hooks in the manual and provide a pull-request to improve PyInstaller.

    I'm not a Python programmer and inherited this project. I have no idea how to proceed. Is anyone familiar with this problem?

    Based on other searches on this site/web I tried adding an additional hook:

    from PyInstaller.utils.hooks import qt_plugins_binaries
    # Fixed the issue: could not find or load the Qt platform plugin "windows".
    binaries = qt_plugins_binaries('platforms', 'PyQt5')


    That also failed. I updated version of pyqt (now 5.9) reinstalled pyInstaller in my virtual environment. Exe is created fine up until I try running it with the Pandas import.

    Updated Pandas based on this answer- ImportErrorWhenRunningHook: pyinstaller failed to import module _pyinstaller_hooks_0_pandas_io_formats_style required for module

    But with no progress.

    I run this with pyinstaller --onefile --debug=all main.py

    Minimal Reproducible Example:

    Main.py

    import sys
    import os

    from gui import Window

    def api():
    w = Window()
    w.window.mainloop()

    if __name__ == '__main__':
    api()


    gui.py

    """This is the GUI object"""

    from tkinter import Tk, Label, Button, Entry, filedialog, HORIZONTAL
    from tkinter.ttk import Progressbar
    from threading import Thread


    from re import sub
    from pandas import read_excel, DataFrame

    class Window():
    def __init__(self):
    self.window = Tk()
    self.window.title('Report Analysis')
    self.window.geometry("480x640")
    self.window.config(background='white')
    self.widgets = []
    for text in ['Select a File', 'Sheet name', 'Select an Output Directory']:
    self.widgets.append(self.CreateLabel(text))
    self.widgets.insert(1, self.CreateButton('Browse Files', self.BrowseFiles))
    self.widgets.insert(3, Entry(self.window, bd=3, width=18))
    self.widgets.insert(5, self.CreateButton('Browse Directories', self.BrowseDirs))
    self.widgets.append(Label(self.window, text='', bg='white'))
    self.widgets.append(Button(self.window, text='Submit', command=self.Submit))
    for widget in self.widgets:
    index = self.widgets.index(widget)
    pady = ((index + 1) % 2 * 20, (index + 1) % 2 * 5)
    widget.grid(column=0, row=index, padx=50, pady=pady)
    self.progress = Progressbar(self.window, orient=HORIZONTAL, length=200, mode='indeterminate')

    def CreateLabel(self, text):
    return Label(self.window, text=text, width=50, height=2, fg='blue')

    def CreateButton(self, text, command):
    return Button(self.window, text=text, command=command, bd=3, width=18)

    def BrowseFiles(self):
    pass

    def BrowseDirs(self):
    pass

    def Submit(self):
    pass

    def ErrorHandling(self, import_file, sheet_name, output_path):
    if import_file == 'Select a File':
    raise Exception('No file selected.')
    if sheet_name.strip() == '':
    raise Exception('No sheet name entered.')
    if output_path == 'Select an Output Directory':
    raise Exception('No directory selected.')


    Thanks in advance for your time

    Continue reading...

Compartilhe esta Página