- 1 year ago
ortools DLL load failed using PyInstaller
If you are encountering a "DLL load failed" error when using PyInstaller with ortools, it is likely due to the dynamic linking dependencies of ortools not being properly bundled by PyInstaller. Here are some steps you can take to resolve the issue:
Specify ortools' hidden imports: PyInstaller may not automatically detect and include all the necessary ortools dependencies. You can specify the required hidden imports explicitly to ensure they are bundled correctly. Create a
.spec
file for your PyInstaller project (if you don't have one already) using the--specpath
option. Then, open the.spec
file in a text editor and add the following line to thehiddenimports
section:pythonhiddenimports = ['ortools.constraint_solver.routing_enums_pb2']
Replace
ortools.constraint_solver.routing_enums_pb2
with any other necessary ortools modules or protobuf generated modules that your code relies on. Save the.spec
file.Build the executable with the
.spec
file: Use thepyinstaller
command with the--specpath
option to specify the path to the.spec
file you created. For example:bashpyinstaller --specpath /path/to/specfile your_script.spec
Replace
/path/to/specfile
with the actual path to your.spec
file, andyour_script.spec
with the name of your.spec
file.Test the executable: Run the generated executable and verify if the "DLL load failed" error is resolved.
Manually copy ortools DLLs: If the above steps did not resolve the issue, you can try manually copying the necessary ortools DLLs alongside the generated executable. Locate the required DLL files (such as
ortools.dll
,libprotobuf.dll
, etc.) in the ortools installation directory (e.g.,C:\Python\Python38\Lib\site-packages\ortools
) and copy them into the same directory as the generated executable.Note: Make sure the ortools DLLs you copy match the architecture (32-bit or 64-bit) of your Python installation and the PyInstaller-generated executable.
Try running the executable again and check if the "DLL load failed" error is resolved.
If the issue persists, ensure that your ortools installation is compatible with the Python version you are using and that it is properly installed and functioning outside of PyInstaller. You may also consider reaching out to the ortools community or support channels for further assistance with PyInstaller integration.