Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem with hl7apy when app is built with PyInstaller #140

Open
paw-bor opened this issue Dec 31, 2024 · 2 comments
Open

Problem with hl7apy when app is built with PyInstaller #140

paw-bor opened this issue Dec 31, 2024 · 2 comments

Comments

@paw-bor
Copy link

paw-bor commented Dec 31, 2024

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?

@svituz
Copy link
Member

svituz commented Jan 7, 2025

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.

@paw-bor
Copy link
Author

paw-bor commented Jan 8, 2025

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants