Pyinstaller未加载DLL [英] Pyinstaller not loading DLL

查看:64
本文介绍了Pyinstaller未加载DLL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为我的python脚本 dungeon.py 构建一个exe后,当 PyBearLibTerminal.py 时出现错误(我的程序导入的脚本)尝试加载DLL BearLibTerminal.dll 。该脚本运行良好,仅运行导致错误的可执行文件。运行可执行文件时报告的错误是

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.

这是 PyBearLibTerminal.py 导致错误的文件。

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.")

最后,这是我的 dungeon.spec 文件:

# -*- 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')


推荐答案

1)检查dist /文件夹(或.exe所在的任何文件夹),以确保BearLibTerminal.dll存在并且可以访问。如果您的.exe不存在,您的.exe将无法运行

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)接下来,检查依赖项。它不一定能找到BearLibTerminal.dll,但不能找到BearLibTerminal.dll所依赖的事实。如果您的计算机上安装了Visual Studio,请使用dumpbin来确定DLL所依赖的内容。 (dumpbin安装在此处C:\Program Files(x86)\Microsoft Visual Studio 14.0\VC\bin\dumpbin.exe)

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

这将列出BearLibTerminal.dll的DLL依赖于取决于。如果适用,请确保将它们包含在与exe相同的目录中(例如,kernel32.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)没有Visual Studio或dumpbin,请下载 http://www.dependencywalker.com/ ,它将完成相同的事情

3) if you don't have Visual Studio or dumpbin, download http://www.dependencywalker.com/ and it will accomplish the same thing

4)如果您确定所有DLL及其所依赖的DLL都已考虑在内,请检查您的spec文件以确保其格式正确。如果在运行pyinstaller时显示一堆警告,可能会导致问题。特别是在Windows 10上(它们不能很好地发挥作用: https://github.com/pyinstaller / pyinstaller / issues / 1566

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)如果BearLibTerminal.dll是您自己编译的dll,请确保它是在发布模式下构建的,并且C / C ++代码生成运行时库为/ MT(多线程)

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)确保您也拥有最新版本的pyinstaller。

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.

这篇关于Pyinstaller未加载DLL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆