Search code examples
pythonpyqt5pyinstaller

How do I create an exe file of gui made using pyqt5?(There are several ui files)


I tried to create an exe file.

I have several ui files, of which I tried to make user_view_ui.py, which acts as a main_window, an executable file.

Step 1) user_view_ui.py

def resource_path(relative_path):
    """ Get absolute path to resource, works for dev and for PyInstaller """
    base_path = getattr(sys, '_MEIPASS', os.path.dirname(os.path.abspath(__file__)))
    return os.path.join(base_path, relative_path)

form_ui = uic.loadUiType("ui\\user_view.ui")[0]

class UserViewUi(QMainWindow, form_ui):
...

And, I added the "resource_path" function in the others python files.

Step2) I modified user_view_ui.spec file.

ui_list = ['.\\ui\\user_view.ui', '.\\ui\\change_list_view.ui', '.\\ui\\move_to_another_changelist.ui', '.\\ui\\login.ui', '.\\ui\\user_settings.ui']

a = Analysis(['user_view_ui.py'],
             pathex=[],
             binaries=[],
             datas=ui_list,

but, when I execute the "pyinstaller --onefile user_view_ui.spec", below the errors occur.

Traceback (most recent call last):
  File "c:\users\appdata\local\programs\python\python36\lib\runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "c:\users\appdata\local\programs\python\python36\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "C:\Users\AppData\Local\Programs\Python\Python36\Scripts\pyinstaller.exe\__main__.py", line 7, in <module>
  File "c:\users\appdata\local\programs\python\python36\lib\site-packages\PyInstaller\__main__.py", line 124, in run
    run_build(pyi_config, spec_file, **vars(args))
  File "c:\users\appdata\local\programs\python\python36\lib\site-packages\PyInstaller\__main__.py", line 58, in run_build
    PyInstaller.building.build_main.main(pyi_config, spec_file, **kwargs)
  File "c:\users\appdata\local\programs\python\python36\lib\site-packages\PyInstaller\building\build_main.py", line 803, in main
    build(specfile, distpath, workpath, clean_build)
  File "c:\users\appdata\local\programs\python\python36\lib\site-packages\PyInstaller\building\build_main.py", line 725, in build
    exec(code, spec_namespace)
  File "user_view_ui.spec", line 20, in <module>
    noarchive=False)
  File "c:\users\appdata\local\programs\python\python36\lib\site-packages\PyInstaller\building\build_main.py", line 287, in __init__
    for name, pth in format_binaries_and_datas(datas, workingdir=spec_dir):
  File "c:\users\appdata\local\programs\python\python36\lib\site-packages\PyInstaller\building\utils.py", line 506, in format_binaries_and_datas
    for src_root_path_or_glob, trg_root_dir in binaries_or_datas:
ValueError: too many values to unpack (expected 2)

Solution

  • In your .spec file the datas field expects a list of tuples containing the path to data file and the target folder it should be copied into at execution time.

    for example:

    ui_list = [
    ('.\\ui\\user_view.ui','.\\ui'), 
    ('.\\ui\\change_list_view.ui','.\\ui'), 
    ('.\\ui\\move_to_another_changelist.ui','.\\ui'),
    ('.\\ui\\login.ui', '.\\ui'),
    ('.\\ui\\user_settings.ui','.\\ui')
    ]
    
    a = Analysis(['user_view_ui.py'],
                 pathex=[],
                 binaries=[],
                 datas=ui_list,
    

    You can try using relative paths in your resource function.

    def resource_path(relative_path):
        base_path = os.path.dirname(os.path.abspath(__file__))
        uipath = os.path.join(base_path, relative_path)
        return os.path.relpath(uipath, '.')
    
    form_ui = uic.loadUiType(os.path.join(resource_path("ui"),"user_view.ui"))[0]