You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

使用PyInstaller生成.exe时因pylibdmtx动态库缺失报错求助

Fixing PyInstaller Freeze Issues with pylibdmtx

Hey there! I’ve dealt with this exact problem when using pylibdmtx alongside PyInstaller, so let’s break down what’s happening and how to fix it.

Why This Happens

The core issue is that pylibdmtx relies on an underlying dynamic library (libdmtx.dll on Windows) that PyInstaller doesn’t automatically detect and package with your executable. When you freeze your app, this missing DLL causes the "dynlib/dll was not found" error, and the line 9 exception is just a side effect of the library failing to load properly.

Solution 1: Manually Copy the DLL

This is the quickest fix for a one-off build:

  • Locate the libdmtx.dll file in your Python environment. It’s usually in:
    C:\PythonXX\Lib\site-packages\pylibdmtx\lib (replace XX with your Python version)
  • After running PyInstaller to generate your .exe, copy this DLL file into the same folder as your executable. When you run the app, it’ll now find the required library.

Solution 2: Automate DLL Packaging with PyInstaller

For a more permanent fix that includes the DLL directly in your build:

Option A: Command Line Argument

When running PyInstaller, use the --add-binary flag to explicitly include the DLL:

pyinstaller --onefile --add-binary "C:\PythonXX\Lib\site-packages\pylibdmtx\lib\libdmtx.dll;." your_script.py
  • The path before the semicolon is the location of your DLL; the part after tells PyInstaller to place it in the same directory as the final .exe.

Option B: Modify the .spec File

  1. First generate a spec file for your project:
    pyinstaller your_script.py
    
  2. Open the generated your_script.spec file, find the binaries list, and add the DLL path:
    a = Analysis(
        ['your_script.py'],
        pathex=[],
        binaries=[('C:\\PythonXX\\Lib\\site-packages\\pylibdmtx\\lib\\libdmtx.dll', '.')],
        datas=[],
        ...
    )
    
  3. Rebuild using the spec file:
    pyinstaller your_script.spec
    

Quick Check to Avoid Further Issues

  • Make sure the libdmtx.dll matches your Python architecture (32-bit vs 64-bit). If your Python is 64-bit, you need the 64-bit DLL, and vice versa—mismatched versions will still throw "not found" errors.

内容的提问来源于stack exchange,提问作者Jay Lee

火山引擎 最新活动