Reputation: 431
After building an exe for my python script dungeon.py
, I am getting an error when PyBearLibTerminal.py
(a script my program imports) tries to load a DLL BearLibTerminal.dll
. The script runs fine, it is only running the executable that causes the error. The error that is reported when running the executable is
[3464] Failed to execute script dungeon
Traceback (most recent call last):
File "dungeon.py", line 2, in <module>
File "<frozen importlib._bootstrap>", line 2237, in _find_and_load
File "<frozen importlib._bootstrap>", line 2226, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 1200, in _load_unlocked
File "<frozen importlib._bootstrap>", line 1129, in _exec
File "C:\Program Files\Python34\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 631, in exec_module
exec(bytecode, module.__dict__)
File "PyBearLibTerminal.py", line 50, in <module>
RuntimeError: BearLibTerminal library cannot be loaded.
This is the beginning of the PyBearLibTerminal.py
file where the error is caused.
import sys, ctypes, numbers, os
_version3 = sys.version_info >= (3, 0)
_library = None
_possible_library_names = [
'BearLibTerminal.dll', # Generic Windows DLL
'./libBearLibTerminal.so', # Local Linux SO
'./libBearLibTerminal.dylib', # Local OS X dylib
'./BearLibTerminal.so', # Local Linux SO w/o prefix
'libBearLibTerminal.so', # System Linux SO
'libBearLibTerminal.dylib', # System OS X dylib
'BearLibTerminal.so' # System Linux SO w/o prefix
]
ctypes.windll.kernel32.SetDllDirectoryW(os.getcwd().replace('\\', '/'))
for name in _possible_library_names:
try:
_library = ctypes.CDLL(name)
break
except OSError:
continue
if _library is None:
raise RuntimeError("BearLibTerminal library cannot be loaded.")
And finally, here is my dungeon.spec
file:
# -*- mode: python -*-
block_cipher = None
a = Analysis(['dungeon.py'],
pathex=['C:\\Users\\Brett\\Documents\\Projects\\Spark'],
binaries=[('BearLibTerminal.dll', '.')],
datas=[('enemies.json', '.'), ('items.json', '.'), ('materials.json', '.'), ('names.json', '.'), ('unifont-8.0.01.ttf', '.')],
hiddenimports=[],
hookspath=[],
runtime_hooks=[],
excludes=[],
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=block_cipher)
pyz = PYZ(a.pure, a.zipped_data,
cipher=block_cipher)
exe = EXE(pyz,
a.scripts,
exclude_binaries=True,
name='dungeon',
debug=False,
strip=False,
upx=True,
console=True )
coll = COLLECT(exe,
a.binaries,
a.zipfiles,
a.datas,
strip=False,
upx=True,
name='dungeon')
Upvotes: 1
Views: 5408
Reputation: 3889
1) Check the dist/ folder (or whatever folder the .exe is) to make sure that the BearLibTerminal.dll is there and ready to be accessed. Your .exe won't run if it isn't there
2) Next check the dependencies. It is not necessarily the fact that it cannot find BearLibTerminal.dll, but it cannot find what BearLibTerminal.dll depends on. If you have Visual Studio installed on your computer, use dumpbin to figure out what your DLL depends on. (dumpbin is installed here C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\bin\dumpbin.exe)
dumpbin /DEPENDENTS your.dll
this wil list the DLLs that BearLibTerminal.dll depends on. Make sure they are included in the same directory as your exe if they apply (kernel32.dll doesn't need to be in that directory for example)
3) if you don't have Visual Studio or dumpbin, download http://www.dependencywalker.com/ and it will accomplish the same thing
4) If you are sure that your DLL and the DLLs it depends on are all accounted for, check your spec file to be sure it properly formatted. If there are a bunch of warnings displayed when running pyinstaller, that can cause problems. Especially if on Windows 10 (they don't play well: https://github.com/pyinstaller/pyinstaller/issues/1566)
5) If BearLibTerminal.dll is your own dll which you compiled, makes sure it was built in release mode and your C/C++ code generation runtime library is /MT (multi-threaded)
6) Make sure you have the most recent version of pyinstaller as well.
It is a lot of information, but hopefully it can help solve your issue. I was dealing with the very same issue myself.
Upvotes: 1