-
Notifications
You must be signed in to change notification settings - Fork 7.7k
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
fix: load model library when using pyinstaller to build executable fi… #2530
base: main
Are you sure you want to change the base?
Conversation
e0e5b74
to
fe000b7
Compare
… windows Signed-off-by: nathan <[email protected]>
def load_llmodel_library(): | ||
""" | ||
Loads the llmodel shared library based on the current operating system. | ||
|
||
This function attempts to load the shared library using the appropriate file | ||
extension for the operating system. It first tries to load the library with the | ||
'lib' prefix (common for macOS, Linux, and MinGW on Windows). If the file is not | ||
found and the operating system is Windows, it attempts to load the library without | ||
the 'lib' prefix (common for MSVC on Windows). | ||
|
||
Returns: | ||
ctypes.CDLL: The loaded shared library. | ||
|
||
Raises: | ||
OSError: If the shared library cannot be found. | ||
""" | ||
# Determine the appropriate file extension for the shared library based on the platform | ||
ext = {"Darwin": "dylib", "Linux": "so", "Windows": "dll"}[platform.system()] | ||
|
||
# Define library names with and without the 'lib' prefix | ||
library_name_with_lib_prefix = f"libllmodel.{ext}" | ||
library_name_without_lib_prefix = "llmodel.dll" | ||
base_path = MODEL_LIB_PATH | ||
|
||
try: | ||
# macOS, Linux, MinGW | ||
lib = ctypes.CDLL(str(MODEL_LIB_PATH / f"libllmodel.{ext}")) | ||
except FileNotFoundError: | ||
if ext != 'dll': | ||
# Attempt to load the shared library with the 'lib' prefix (common for macOS, Linux, and MinGW) | ||
lib = ctypes.CDLL(str(base_path / library_name_with_lib_prefix)) | ||
except OSError: # OSError is more general and includes FileNotFoundError | ||
if ext != "dll": | ||
raise | ||
# MSVC | ||
lib = ctypes.CDLL(str(MODEL_LIB_PATH / "llmodel.dll")) | ||
# For Windows (ext == 'dll'), attempt to load the shared library without the 'lib' prefix (common for MSVC) | ||
lib = ctypes.CDLL(str(base_path / library_name_without_lib_prefix)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Documentation is great, but this is a bit much. All this change really needs to be is:
- Replace FileNotFoundError with OSError
- Add a comment mentioning the specific OSError that appears when using pyinstaller (it will at least have an error code, but often it is a specific subclass of OSError)
- Example:
except OSError: # e.g. FileNotFoundError, PermissionError (pyinstaller)
- Example:
- Make the quotes consistent
- Add a concise docstring - a sentence or two is more than enough
Anything else is unnecessary and IMO too verbose.
Ideally the except
clause is as narrow as possible. If there is a specific exception that applies:
try:
...
except (FileNotFoundError, PermissionError): # pyinstaller raises PermissionError
...
Otherwise:
try:
...
except OSError as e:
if e.errno not in (errno.ENOENT, errno.EPERM): # pyinstaller fails with EPERM
raise
...
And if we're going to add a return type to the docstring, I would prefer we annotate it as well (load_llmodel_library() -> ctypes.CDLL:
).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank a bunch! I will make changes later
I recently discovered that |
Describe your changes
Fix loading llmmodel library when using pyinstaller to build executable binary that includes gpt4all
Issue ticket number and link
Fix pyinstaller issues: #1727
Checklist before requesting a review