python - pyinstaller 给出 "ImportError: DLL load failed"

标签 python bluetooth pyinstaller

当我使用 pyinstaller 运行从 python 编译为 exe 的程序时,出现错误(我使用 python 2.7.16):

Traceback (most recent call last):
  File "bloepie.py", line 1, in <module>
  File "c:\users\stefan\appdata\local\temp\pip-install-v9ecuy\pyinstaller\PyInstaller\loader\pyimod03_importers.py", line 395, in load_module
  File "build\bdist.win-amd64\egg\bluetooth\__init__.py", line 37, in <module>
  File "c:\users\stefan\appdata\local\temp\pip-install-v9ecuy\pyinstaller\PyInstaller\loader\pyimod03_importers.py", line 395, in load_module
  File "build\bdist.win-amd64\egg\bluetooth\msbt.py", line 2, in <module>
  File "c:\users\stefan\appdata\local\temp\pip-install-v9ecuy\pyinstaller\PyInstaller\loader\pyimod03_importers.py", line 395, in load_module
  File "build\bdist.win-amd64\egg\bluetooth\_msbt.py", line 7, in <module>
  File "build\bdist.win-amd64\egg\bluetooth\_msbt.py", line 6, in __bootstrap__
ImportError: DLL load failed: The specified module could not be found.
[21200] Failed to execute script bloepie

我编译它:

pyinstaller bloepie.py --onefile

this person和我有同样的问题,但他得到的唯一答案是他应该升级到 python 3 或更高版本,我不想这样做,而且它没有被标记为正确答案,所以我也不能保证如果我这样做了它会起作用。这个人有一个稍微不同的问题。我有一个“导入错误:DLL 无法加载:...”他有一个正常的导入错误(我不知道这是否有所不同,但我找不到任何相关信息)

这是我用pyinstaller编译的python代码:

import bluetooth
from pynput.mouse import Button, Controller

mouse = Controller()
may_i = 0
may_x = 0
may_y = 0
lockdown = 0

server_sock = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
server_sock.bind(("",bluetooth.PORT_ANY))
server_sock.listen(1)

bluetooth.advertise_service(server_sock, "helloService",
                     service_classes=[bluetooth.SERIAL_PORT_CLASS],
                     profiles=[bluetooth.SERIAL_PORT_PROFILE])

client_sock, address = server_sock.accept()
print "Accepted connection from ",address

while True:
    may_i = 0
    gata = client_sock.recv(1024)
    if gata == "":
        break
    data = gata.split(";")
    for dt in data:
        if dt == "":
            may_i = 1
        if dt == "Down":
            loci = mouse.position
            may_x = 0
            may_y = 0
            lockdown = 0
            may_i = 1
        if dt == "Up":
            if may_x == 1 and may_y == 1 and lockdown == 0:
                mouse.press(Button.left)
                mouse.release(Button.left)
            may_i = 1
        if may_i == 0:
            print "received: %s" % dt
            if dt == "x0.0":
                may_x = 1
            if dt == "y0.0":
                may_y = 1
            if dt != "x0.0" and dt != "y0.0":
                lockdown = 1
                if "x" in dt:
                    dt = dt.replace("x", "")
                    if "-" in dt:
                        dt = dt.replace("-", "")
                        if dt.split(".")[0] != 0:
                            dt = str(float(dt) * 1.5)
                            mouse.position = (mouse.position[0] + int(dt.split(".")[0]), mouse.position[1])
                    else:
                        if dt.split(".")[0] != 0:
                            dt = str(float(dt) * 1.5)
                            mouse.position = (mouse.position[0] - int(dt.split(".")[0]), mouse.position[1])
                if "y" in dt:
                    dt = dt.replace("y", "")
                    if "-" in dt:
                        dt = dt.replace("-", "")
                        if dt.split(".")[0] != 0:
                            dt = str(float(dt) * 1.5)
                            mouse.position = (mouse.position[0], mouse.position[1] + int(dt.split(".")[0]))
                    else:
                        if dt.split(".")[0] != 0:
                            dt = str(float(dt) * 1.5)
                            mouse.position = (mouse.position[0], mouse.position[1] - int(dt.split(".")[0]))
                        
client_sock.close()
server_sock.close()

那么这里出了什么问题呢?为什么编译后的版本不运行? (py 文件工作正常) 谢谢

最佳答案

我尝试过使用钩子(Hook),但它对我的代码效果不佳。相反,我将整个 scipy 文件夹复制到 python 代码文件夹,问题就解决了。在我的电脑中,scipy 文件夹位于 "D:\Programe\Anaconda\Lib\site-packages"

希望对您有所帮助。

综上所述,我在使用pyinstaller打包的技巧如下:

  1. 需要在spec文件开头添加如下代码:
import sys  
sys.setrecursionlimit(5000)
  1. 复制高危模块到打包文件夹,避免DLL丢失错误。 (在我的工作中,Numpy、Scipy 和 Pandas 被复制了。)

  2. 对于模块丢失等其他错误,您可以通过将模块添加到 spec 文件的 hiddenimports 中来修复错误。

  3. 需要将 Pandas 更新到 1.x.x 版本。 Pandas 0.24.x 版本将显示“No module named 'pandas._libs.tslib'”并且无法通过 hiddenimports 修复。

关于python - pyinstaller 给出 "ImportError: DLL load failed",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57935948/

相关文章:

django - Windows 上带有 pyinstaller 的独立 Django 应用程序

python - 理解 python 中生成器的执行顺序

python - 生成器函数性能

python - TF Hub微调错误: ValueError: Failed to find data adapter that can handle input

java - Android 中的 stringAsBytes 未知函数

python - 您为python推荐哪种构建工具?

python - 如何获取恒定大小的 python 列表的随机切片。 (最小代码)

ios - 如何通过外部附件框架使用蓝牙 PAN 配置文件

linux - 蓝牙RFCOMM连接Linux

python - PyInstaller 创建的独立可执行文件无法导入 Numpy