无法使用模块 pdftotext 构建独立的 .exe

Can't build a standalone .exe with the module pdftotext

我正在尝试将包含模块 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 使用的库)有关,但目前我无法理解哪个文件挂钩到 import poppler

编辑 2. 经过一些工作后,我发现两件事可能有助于更好地了解我的情况:

  1. .exe 应用程序可以在我的设备上运行(即使在我安装了 poppler 和 pdftotext 的 anaconda 环境之外)但它不能在其他设备上运行(我尝试了两种不同的 windows laptop 和报错是一样的);没有 'pdftotext' 的脚本适用于所有设备

  2. 在 dist 文件夹(由 pyinstaller 构建)中出现一个文件 名称 pdftotext:文件是 'pdftotext.cp37-win_amd64.pyd'(我是 不确定它是什么)。在我的 anaconda env 中只有两个文件 其中包含字符串 'pdftotext':文件是 'pdftotext.cp37-win_amd64.pyd' 和 '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:\Users\miche\Desktop\project>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:\Users\miche\Desktop\project\main.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:\users\miche\anaconda3\envs\envpdf\python.exe
3608 INFO: Analyzing C:\Users\miche\Desktop\project\main.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:\users\miche\anaconda3\envs\envpdf\python37.dll
34390 INFO: Found binding redirects:
[]
34395 INFO: Warnings written to C:\Users\miche\Desktop\project\build\main\warn-main.txt
34430 INFO: Graph cross-reference written to C:\Users\miche\Desktop\project\build\main\xref-main.html
35274 INFO: checking PYZ
35274 INFO: Building PYZ because PYZ-00.toc is non existent
35274 INFO: Building PYZ (ZlibArchive) C:\Users\miche\Desktop\project\build\main\PYZ-00.pyz
35794 INFO: Building PYZ (ZlibArchive) C:\Users\miche\Desktop\project\build\main\PYZ-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:\users\miche\anaconda3\envs\envpdf\lib\site-packages\PyInstaller\bootloader\Windows-64bit\run.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:\Users\miche\Desktop\project\build\main\main.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 的挂钩文件。引用文档:

In summary, a “hook” file extends PyInstaller to adapt it to the special needs and methods used by a Python package. ... ... They help the Analysis phase find needed files.

可以在 https://pyinstaller.readthedocs.io/en/stable/hooks.html.

找到官方 hook 文档

编辑:以下应该有效:

创建此目录结构:

- 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