我正在尝试将包含模块pdftotext的python脚本转换为独立的.exe。当我在我的anaconda环境中测试.exe应用程序时,它工作正常,但当我在另一台设备上测试它时,它会给我这个错误:
File "main.py", line 3, in <module> #line 3 is import pdftotext
"ImportError: DLL load failed: The specified module could not be found"
[7300] Failed to execute script main
我确信这个问题与模块pdftotext有关,因为我尝试了下面的简单脚本,并且工作正常:
a=input("Start")
print("Hello world")
b=input("End")
如果我转换这个脚本,就会出现错误:
import pdftotext
a=input("Inserisci")
print("Hello world")
b=input("Fine")
对不起,我的英语不好,我来自意大利。我希望我能把自己说清楚,感谢的每一个人
编辑1。我发现问题可能与poppler(pdftotext使用的库(有关,但目前我不明白哪个文件连接到导入poppler
编辑2。经过一些工作,我发现了两件可能有助于更好地了解我的情况的事情:
.exe应用程序在我的设备上运行(甚至在我安装了poppler和pdftotext的anaconda env之外(,但在其他设备上不运行(我尝试了两台不同的windows笔记本电脑,错误相同(;没有"pdftotext"的脚本在每个设备上工作
在dist文件夹(由pyinstaller构建(中,出现一个包含名称pdftotext:文件是"pdftotext.cp37-winamd64.pyd"(我是不确定是什么(。在我的anaconda env中只有两个文件包含字符串"pdftotext":文件为"pdftotext.cp37-win_amd64.pyd"one_answers"pdftotext.exe">
编辑3在不同的设备上运行main.exe时出现完全错误:
Traceback (most recent call last):
File "main.py",line 1, in <module>
ImportError: DLL load failed: The specified module could not be found
[7140] Failed to execute script main
完整的pyinstaller日志:
(envPDF) C:UsersmicheDesktopproject>pyinstaller --additional-hooks-dir=hooks main.py
65 INFO: PyInstaller: 3.6
65 INFO: Python: 3.7.6 (conda)
65 INFO: Platform: Windows-10-10.0.18362-SP0
65 INFO: wrote C:UsersmicheDesktopprojectmain.spec
65 INFO: UPX is not available.
81 INFO: Extending PYTHONPATH with paths
['C:\Users\miche\Desktop\project', 'C:\Users\miche\Desktop\project']
81 INFO: checking Analysis
81 INFO: Building Analysis because Analysis-00.toc is non existent
81 INFO: Initializing module dependency graph...
81 INFO: Caching module graph hooks...
81 INFO: Analyzing base_library.zip ...
3232 INFO: Caching module dependency graph...
3326 INFO: running Analysis Analysis-00.toc
3343 INFO: Adding Microsoft.Windows.Common-Controls to dependent assemblies of final executable
required by c:usersmicheanaconda3envsenvpdfpython.exe
3608 INFO: Analyzing C:UsersmicheDesktopprojectmain.py
3624 INFO: Processing module hooks...
3624 INFO: Loading module hook "hook-encodings.py"...
3718 INFO: Loading module hook "hook-pydoc.py"...
3718 INFO: Loading module hook "hook-xml.py"...
3954 INFO: Loading module hook "hook-pdftotext.py"...
6537 INFO: Determining a mapping of distributions to packages...
29442 INFO: Packages required by pdftotext:
[]
33735 INFO: Looking for ctypes DLLs
33735 INFO: Analyzing run-time hooks ...
33746 INFO: Looking for dynamic libraries
34387 INFO: Looking for eggs
34387 INFO: Using Python library c:usersmicheanaconda3envsenvpdfpython37.dll
34390 INFO: Found binding redirects:
[]
34395 INFO: Warnings written to C:UsersmicheDesktopprojectbuildmainwarn-main.txt
34430 INFO: Graph cross-reference written to C:UsersmicheDesktopprojectbuildmainxref-main.html
35274 INFO: checking PYZ
35274 INFO: Building PYZ because PYZ-00.toc is non existent
35274 INFO: Building PYZ (ZlibArchive) C:UsersmicheDesktopprojectbuildmainPYZ-00.pyz
35794 INFO: Building PYZ (ZlibArchive) C:UsersmicheDesktopprojectbuildmainPYZ-00.pyz completed successfully.
35802 INFO: checking PKG
35802 INFO: Building PKG because PKG-00.toc is non existent
35804 INFO: Building PKG (CArchive) PKG-00.pkg
35824 INFO: Building PKG (CArchive) PKG-00.pkg completed successfully.
35824 INFO: Bootloader c:usersmicheanaconda3envsenvpdflibsite-packagesPyInstallerbootloaderWindows-64bitrun.exe
35824 INFO: checking EXE
35824 INFO: Building EXE because EXE-00.toc is non existent
35824 INFO: Building EXE from EXE-00.toc
35824 INFO: Appending archive to EXE C:UsersmicheDesktopprojectbuildmainmain.exe
35824 INFO: Building EXE from EXE-00.toc completed successfully.
35875 INFO: checking COLLECT
35875 INFO: Building COLLECT because COLLECT-00.toc is non existent
35875 INFO: Building COLLECT COLLECT-00.toc
96644 INFO: Building COLLECT COLLECT-00.toc completed successfully.
您需要的是PyInstaller的挂钩文件。引用文件:
总之,一个"hook"文件扩展了PyInstaller,使其适应Python包所使用的特殊需求和方法。。。它们帮助分析阶段查找所需的文件。
官方钩子文档可以在https://pyinstaller.readthedocs.io/en/stable/hooks.html.
编辑:以下操作应该有效:
创建此目录结构:
- yourcode.py
- hooks
- hook-pdftotext.py
在挂钩文件中放入以下内容:
from PyInstaller.utils.hooks import collect_all
datas, binaries, hiddenimports = collect_all('pdftotext')
然后构建:
$ pyinstaller --additional-hook-dir=hooks yourcode.py