Problem with hl7apy when app is built with PyInstaller
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
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?
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.
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