Include poppler while generating an application using pyinstalle

ghz 15hours ago ⋅ 7 views

Include poppler while generating an application using pyinstaller

I am using poppler to read pdfs

Poppler folder is located with my source file as shown below. Code below:

    pages = convert_from_path(pdf_file,500,poppler_path = r'.\\poppler\\bin',
                              first_page=pageNo[0],last_page =pageNo[len(pageNo)-1] )

I want to generate the exe for my python application using pyinstaller. However i include them in pyinstaller command.It doesnt seem to work.

I get the below error: Exception Unable to get page count. Is poppler installed and in PATH? occurred while creating the DB dataframes

Below are the ways i have tried to include the poppler in my pyinstaller command:

pyinstaller --noconfirm --onefile --windowed --add-data --add-data "C:/Users/projectPath/poppler/*;./poppler" "C:/Users/projectPath/AutoUpdate.py"

pyinstaller --noconfirm --onefile --windowed --add-data --add-data "C:/Users/projectPath/poppler;./poppler" "C:/Users/projectPath/AutoUpdate.py"

pyinstaller --noconfirm --onefile --windowed --add-data --add-data "C:/Users/projectPath/poppler;poppler/" "C:/Users/projectPath/AutoUpdate.py"

Answer

The error occurs because the poppler_path you specified is not being correctly bundled or accessed by the executable created by PyInstaller. To resolve this, you need to ensure the poppler folder is bundled correctly and the poppler_path is correctly referenced in your code after the application is packaged.

Here’s how to fix it:

1. Correct --add-data Path Syntax

In the --add-data flag, the syntax should correctly specify the source directory and the destination directory separated by a semicolon (;) on Windows or a colon (:) on other platforms. For example:

--add-data "C:/Users/projectPath/poppler;poppler"

2. Access the poppler_path Dynamically

When the application is executed as a standalone executable, the sys._MEIPASS attribute contains the temporary directory where bundled resources are extracted. Update your code to dynamically determine the path to poppler:

import os
import sys

if getattr(sys, 'frozen', False):  # Check if running as a PyInstaller bundle
    base_path = sys._MEIPASS
else:
    base_path = os.path.dirname(__file__)

poppler_path = os.path.join(base_path, 'poppler', 'bin')

pages = convert_from_path(pdf_file, 500, poppler_path=poppler_path,
                          first_page=pageNo[0], last_page=pageNo[-1])

3. Build the Executable

Run PyInstaller with the updated --add-data command:

pyinstaller --noconfirm --onefile --windowed --add-data "C:/Users/projectPath/poppler;poppler" "C:/Users/projectPath/AutoUpdate.py"

4. Verify the Packaged Application

  1. After building the executable, ensure the poppler folder is bundled within the temporary directory used by PyInstaller (sys._MEIPASS).
  2. Confirm that the poppler_path points to the correct directory when the executable is run.

Debugging Tips

  • If you still encounter issues, try using --debug all with PyInstaller to inspect how files are bundled.
  • Verify the poppler binaries are included in the final executable package by checking the dist/your_executable/ directory.

This approach ensures that the poppler_path is always correctly resolved, regardless of whether your script is run directly or as a standalone executable.