Xavier Olive research teaching python blog til cli

Issues on loading DLLs with pyinstaller

9 September 2015

There seems to be issues upon loading some DLLs when running an executable produced with pyinstaller, esp. with the --onefile option.
Some DLLs seem not to be wrapped and found by the executable.

You can manually add them in the generated spec file: for each DLL file f, append the following to a.binaries.

a.binaries += (f, os.path.join(path_to_f, f), 'BINARY')

This is not enough for Python to find where the DLLs are located.
The following lines are then to be put before importing the problematic modules (or loading the problematic DLLs). Upon unwrapping/launching the application, sys._MEIPASS contains the path to the directory where the executable is unwrapped, with the DLLs inside.

try:
    import sys, win32api
    win32api.SetDllDirectory(sys._MEIPASS)
except:
    pass

Noteworthy reference: