hl7apy icon indicating copy to clipboard operation
hl7apy copied to clipboard

Problem with hl7apy when app is built with PyInstaller

Open paw-bor opened this issue 1 year ago • 2 comments

When I run my_app.py, it works fine. But when I run my_app.exe built with PyInstaller, I encounter an error:

./my_app.exe Traceback (most recent call last): File "my_app.py", line 7, in File "", line 1360, in _find_and_load File "", line 1331, in _find_and_load_unlocked File "", line 935, in _load_unlocked File "PyInstaller\loader\pyimod02_importers.py", line 378, in exec_module File "server.py", line 4, in File "", line 1360, in _find_and_load File "", line 1331, in _find_and_load_unlocked File "", line 935, in _load_unlocked File "PyInstaller\loader\pyimod02_importers.py", line 378, in exec_module File "tools.py", line 3, in File "", line 1360, in _find_and_load File "", line 1331, in find_and_load_unlocked File "", line 935, in load_unlocked File "PyInstaller\loader\pyimod02_importers.py", line 378, in exec_module File "hl7apy_init.py", line 334, in File "hl7apy_init.py", line 331, in _discover_libraries FileNotFoundError: [WinError 3] System nie może odnaleźć określonej ścieżki: 'C:\Users\user~1\AppData\Local\Temp\_MEI86002\hl7apy' [PYI-15820:ERROR] Failed to execute script 'my_app' due to unhandled exception!

A workaround is to use the --add-data clause during the build process: pyinstaller my_app.py --onefile --add-data "C:\venv\Lib\site-packages\hl7apy;hl7apy" However, this solution isn't ideal. I suspect the issue might lie in the _discover_libraries function.

Could you please fix it?

paw-bor avatar Dec 31 '24 13:12 paw-bor

Hi @paw-bor, I have no experience with PyInstaller so I don't know what the problem is with it or if it is caused by the library. May it be the --onefile option? The discover_libraries function uses os.path functions to check the versions' directories in a dynamic way so if everything is packed together it won't find the directories.

svituz avatar Jan 07 '25 15:01 svituz

Hi @svituz Yes, I think the problem is due to the --onefile option. As far as I know, the __file__ doesn't work correctly within the PyInstaller environment. Therefore, we should use the _MEIPASS directory to access the application's resources.

I think, the solution might be to modify the discover_libraries function as follows:

def _discover_libraries():
    base_path = getattr(sys, '_MEIPASS', os.path.dirname(__file__))
    library_path = os.path.join(base_path, 'libraries')
    if os.path.exists(library_path):
        return library_path
    return None

paw-bor avatar Jan 08 '25 15:01 paw-bor