无法使用模块pdftotext

时间:2020-03-31 17:52:58

标签: anaconda pyinstaller pdftotext poppler

我正在尝试将包含pdftotext模块的python脚本转换为独立的.exe。 当我在anaconda env中测试.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.经过一些工作,我发现两件事可能有助于更好地了解我的处境:

  1. .exe应用程序可在我的设备上运行(即使在我已安装poppler和pdftotext的anaconda env外部),但无法在其他设备上运行(我尝试了两种不同的Windows笔记本电脑,并且出现了错误一样);没有“ pdftotext”脚本的脚本可在所有设备上使用

  2. 在dist文件夹(由pyinstaller构建)中,出现一个带有 pdftotext名称:文件为“ pdftotext.cp37-win_amd64.pyd”(我是 不知道是什么)。在我的Anaconda环境中,只有两个文件 其中包含字符串“ 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.

1 个答案:

答案 0 :(得分:0)

您需要的是PyInstaller的挂钩文件。引用文档:

总而言之,“挂钩”文件扩展了PyInstaller的功能,以使其适应Python软件包使用的特殊需求和方法。 ……它们帮助分析阶段找到所需的文件。

可以在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