[Python] EXE build using pyinstaller crashes during usage (PyQt5 app)

Discussão em 'Python' iniciado por Stack, Janeiro 28, 2025.

  1. Stack

    Stack Membro Participativo

    The structure of my project directory is as follows:

    --data
    -device_index.json
    -settings.json
    --reports
    --ui
    --tests
    --utilities
    -app.py


    I am trying to use pyinstaller to build an exe for this PyQt5 app. The entry point of the app is app.py.

    I use the following command to build the app.

    pyinstaller --name measurement_automater --onefile --add-data "data/device_index.json;data" --add-data "data/settings.json;data" app.py


    The build process completes successfully. The app also runs. But when I try to run some functionality of the app, it crashes with the following error:

    FileNotFoundError: [Errno 2] No such file or directory: 'C:\\alom_V3_24-01-main\\dist\\.\\data\\device_index.json

    I also modified my code where I am using paths to device_index.json and settings.json using the following code:

    import os
    import sys

    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)


    I am using the above function in my code and calling it like this:

    resource_path("data/device_index.json")
    OR
    resource_path("data/settings.json")


    I also tried using sys._MEIPASS2 instead of sys._MEIPASS but I get the same error.

    Can someone please help and let me know what is going wrong here?

    Thanks in advance!

    Continue reading...

Compartilhe esta Página