Search code examples
pythonpyinstaller

Issue when packing a python script with pypdfium2 into exe using pyinstaller


I have several Python scripts which use pypdfium2 library. They work correctly when I run them with Python. But when I pack them into ".exe" using pyinstaller and try to run them, I get the below error:

    Traceback (most recent call last):
      File "app.py", line 3, in <module>
      File "PyInstaller\loader\pyimod02_importers.py", line 419, in exec_module
      File "pypdfium2\__init__.py", line 7, in <module>
      File "PyInstaller\loader\pyimod02_importers.py", line 419, in exec_module
      File "pypdfium2\_helpers\__init__.py", line 4, in <module>
      File "PyInstaller\loader\pyimod02_importers.py", line 419, in exec_module
      File "pypdfium2\_helpers\unsupported.py", line 8, in <module>
      File "PyInstaller\loader\pyimod02_importers.py", line 419, in exec_module
      File "pypdfium2\raw.py", line 495, in <module>
      File "pypdfium2\raw.py", line 183, in __call__
    ImportError: Could not load pdfium.
    [10640] Failed to execute script 'app' due to unhandled exception!

I read in the thread below that additional data needs to be supplied when I am packing a python script using pypdfium2. The answer lists paths to files which need to be added:

pdfium library is not working when compiling my code in python

However, it seems to me that something changed in the meantime (new version of pypdfium was released on May 9 this year), and I can no longer find the files listed in the above answer ('version.json' file is no longer in lib\site-packages\pypdfium2_raw).

Does anyone have a solution?

Example code which fails when packed into .exe:

    import pypdfium2 as pdfium

    pdf_path = input('Enter the path of the PDF file: ')
    pdf_images = pdfium.PdfDocument(pdf_path)
    # rest of code goes here

Solution

  • I tried to troubleshoot this myself per comment above from Mohammed Almalki. I did find a solution which worked for me. I am noting it here in case someone else has the same issue.

    The only library which needs to be included right now for packing script with pypdfium2 using pyinstaller is 'pdfium.dll'. Pyinstaller should be called like this (on Windows):

    pyinstaller --onefile --add-binary "env\Lib\site-packages\pypdfium2\pdfium.dll;." script.py