2

I'm trying to convert my python script, which contains the module pdftotext, into a standalone .exe. When I test the .exe app in my anaconda env It works correctly but when I test it on another device It gaves me this error:

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

I'm sure the problem concern the module pdftotext because I tried with the simple script below and works correctly:

a=input("Start")
print("Hello world")
b=input("End")

The error appears if I convert this script:

import pdftotext
a=input("Inserisci")
print("Hello world")
b=input("Fine")

Sorry for my poor english, I come from Italy. I hope I'm making myself clear, thanks to everyone

EDIT 1. I figured out the problem may be related to poppler (library used by pdftotext) but at the moment I can't understand which file hooks up to import poppler

EDIT 2. After some work I found out two thing that might help to understand better my situation:

  1. The .exe application works on my device (even outside the anaconda env where I've installed poppler and pdftotext) but It doesn't work on others devices (I've tried two different windows laptop and the error is the same); the script without 'pdftotext' work on every devices

  2. In the dist folder (build by pyinstaller) appears a single file with the name pdftotext: the file is 'pdftotext.cp37-win_amd64.pyd' (i'm not sure what is it). In my anaconda env there are only two file which contains the string 'pdftotext': the files are 'pdftotext.cp37-win_amd64.pyd' and 'pdftotext.exe'

EDIT 3 Full error when I run main.exe on different device :

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

Full pyinstaller log:

(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.
Michele
  • 95
  • 8

1 Answers1

1

What you need is a hook file for PyInstaller. To quote the documentation:

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.

The official hook docs can be found at https://pyinstaller.readthedocs.io/en/stable/hooks.html.

Edit: the following should work:

Create this directory structure:

- yourcode.py
- hooks
  - hook-pdftotext.py

And in the hook file put the following:

from PyInstaller.utils.hooks import collect_all

datas, binaries, hiddenimports = collect_all('pdftotext')

And then build with:

$ pyinstaller --additional-hook-dir=hooks yourcode.py
Legorooj
  • 2,646
  • 2
  • 15
  • 35
  • I tried to figure out which hook i need without success – Michele Apr 01 '20 at 12:01
  • @Michele I've edited. If the update works, please select the answer as correct with the tick underneath the voting buttons. – Legorooj Apr 02 '20 at 04:15
  • Thanks to help me. I tried your tip but I still have the same error. I edit my question to add some news – Michele Apr 02 '20 at 09:20
  • @Michele can you copy and paste the whole error? I'd like to see if there's anything else I can spot; `collect_all` rarely fails. – Legorooj Apr 02 '20 at 09:37
  • I post them above. The dist folder now is bigger because include the 'site-packages' folder but the error is still the same. Thanks to your tip, in the pyinstaller log now appear: 29442 INFO: Packages required by pdftotext: []. but it seems that the result is empty – Michele Apr 02 '20 at 11:44
  • If it isn't too much to ask, can you try to build an .exe with pdftotext module to check if it is my fault or not? Thank you – Michele Apr 02 '20 at 16:47
  • @Michele I've looked into it and figured out the problem. `pdftotext` uses a library called poppler. That's a C library which isn't embedded in the exe because PyInstaller doesn't know about it. It's possible to fix that, by writing a hook file that finds and copies the DLL. However, seeing as I don't use conda, it would take me days - time I don't have. All the information you need is online; find the poppler DLLs, and add them to `binaries` in the hook file. I'd help more, but I don't have the time. – Legorooj Apr 03 '20 at 00:45
  • Also, poppler is GPL licenced. Just in case you didn't know, that means that it's illegal to distribute your code without publishing the source. – Legorooj Apr 03 '20 at 00:49
  • Ok thank you, no problem. I will try following your tips – Michele Apr 03 '20 at 07:08
  • @Michele Where did you get `poppler` binary file? I don't see any `.dll` file installed in my conda environment that has `poppler` installed. – user8491363 Feb 21 '21 at 14:53
  • In my case, the .dll files are: C:\Users\anaconda3\envs\envTEMP\Library\include. However, I just tried and adding manually the hook files is no longer required for pdftotext. Simply try "pyinstaller --onefile --hidden-import=pkg_resources.py2_warn test.py" in a conda env where you have installed both pdftotext (pip install pdftotext) and poppler (conda install -c conda-forge poppler). Let me know if it works for you – Michele Feb 23 '21 at 17:07