使用PyInstaller生成.exe时因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.dllfile in your Python environment. It’s usually in:C:\PythonXX\Lib\site-packages\pylibdmtx\lib(replaceXXwith 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
- First generate a spec file for your project:
pyinstaller your_script.py - Open the generated
your_script.specfile, find thebinarieslist, and add the DLL path:a = Analysis( ['your_script.py'], pathex=[], binaries=[('C:\\PythonXX\\Lib\\site-packages\\pylibdmtx\\lib\\libdmtx.dll', '.')], datas=[], ... ) - Rebuild using the spec file:
pyinstaller your_script.spec
Quick Check to Avoid Further Issues
- Make sure the
libdmtx.dllmatches 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




