PyInstaller + UI Files - FileNotFoundError: [Errno 2] No such file or directory:

前端 未结 2 1420
南旧
南旧 2020-12-03 14:21

I\'m trying to export my .py script to .exe using PyInstaller, which has dependencies on .ui files which were created using Qt Designer.

I can confirm that my .p

相关标签:
2条回答
  • 2020-12-03 15:09

    You can simply use:

    uic.loadUi(r'E:\Development\Python\your_ui.ui', self)
    

    Use the full path, and use pyinstaller with standard arguments, and it works fine. The r prefix makes sure the backslashes are interpreted literally.

    0 讨论(0)
  • 2020-12-03 15:15

    After scratching my head all weekend and looking further on SO, I managed to compile the standalone .exe as expected using the UI files.

    Firstly, I defined the following function using this answer

    Bundling data files with PyInstaller (--onefile)

    # Define function to import external files when using PyInstaller.
    def resource_path(relative_path):
        """ Get absolute path to resource, works for dev and for PyInstaller """
        try:
            # PyInstaller creates a temp folder and stores path in _MEIPASS
            base_path = sys._MEIPASS
        except Exception:
            base_path = os.path.abspath(".")
    
        return os.path.join(base_path, relative_path)
    

    Next I imported the .UI files using this function and variables for the required classes.

    # Import .ui forms for the GUI using function resource_path()
    securitySearchForm = resource_path("securitySearchForm.ui")
    popboxForm = resource_path("popbox.ui")
    
    Ui_MainWindow, QtBaseClass = uic.loadUiType(securitySearchForm)
    Ui_PopBox, QtSubClass = uic.loadUiType(popboxForm)
    

    I then had to create a resource file (.qrc) using Qt Designer and embed images/icons using this resource file. Once done, I used pyrcc4 to convert the .qrc file to .py file, which would be imported in the main script.

    Terminal

    C:\Users\giranm\PycharmProjects\PyQt Tutorial>pyrcc4 -py3 resources.qrc -o resources_rc.py
    

    Python

    import resources_rc
    

    Once I have confirmed the main .py script works, I then created a .spec file using PyInstaller.

    Terminal

    C:\Users\giranm\PycharmProjects\PyQt Tutorial>pyi-makespec --noconsole --onefile secSearch_demo.py
    

    As per PyInstaller's guide, I've added data files by modifying the above .spec file.

    https://pythonhosted.org/PyInstaller/spec-files.html#adding-data-files

    Finally, I then compiled the .exe using the .spec file from above.

    0 讨论(0)
提交回复
热议问题