Problems after generating .exe file from python code to convert pdf to image using pdf2image poppler

I am getting the below error:

Exception in Tkinter callback
Traceback (most recent call last):
File “pdf2image\pdf2image.py”, line 441, in pdfinfo_from_path
File “subprocess.py”, line 854, in init
File “subprocess.py”, line 1307, in _execute_child
FileNotFoundError: [WinError 2] The system cannot find the file specified

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File “tkinter_init_.py”, line 1883, in call
File “PDFtoImage.py”, line 79, in executeApp
File “pdf2image\pdf2image.py”, line 97, in convert_from_path
File “pdf2image\pdf2image.py”, line 467, in pdfinfo_from_path
pdf2image.exceptions.PDFInfoNotInstalledError: Unable to get page count. Is poppler installed and in PATH?

The code snippet that I am executing is below. This code basically converts PDF to image using pdf2image library. In jupyter notebook, its working fine after I have defined the Path of Poppler in System Environment variables as C:\Users<username>\Poppler\bin. But when I am converting the python code to .exe by using pyinstaller doc.py, its throwing error. Kindly help me with this issue.

Import libraries

from PIL import Image
import sys, os
from pdf2image import convert_from_path, convert_from_bytes
from pdf2image.exceptions import (
PDFInfoNotInstalledError,
PDFPageCountError,
PDFSyntaxError
)
import tkinter as tk
from tkinter import *
import poppler

class PDFtoImage(tk.Frame):

def __init__(self, parent):
    tk.Frame.__init__(self, parent)
    self.parent = parent
    self.initialize_user_interface()
    
def initialize_user_interface(self):
    self.parent.geometry("300x300")
    self.parent.title("PDF to Image Converter")
    
    self.label1 = tk.Label(self.parent, text="Source")
    self.label1.place(x=30, y=30)

    self.label2 = tk.Label(self.parent, text="Destination")
    self.label2.place(x=30, y=50)

    self.label3 = tk.Label(self.parent, text="Poppler URL")
    self.label3.place(x=30,y=70)
    
    self.e1 = tk.Entry(self.parent)
    self.e1.place(x=120,y=30)

    self.e2 = tk.Entry(self.parent)
    self.e2.place(x=120,y=50)

    self.e3 = tk.Entry(self.parent)
    self.e3.place(x=120,y=70)
    
    self.b1 = tk.Button(text='Execute', command=self.executeApp)
    self.b1.place(x= 30, y=120)

def executeApp(self):
    x1 = self.e1.get()
    x2 = self.e2.get()
    x3 = self.e3.get()

    ## extracting the Filename from Source Path
    head_tail = os.path.split(x1) 

    # Store all the pages of the PDF in a variable 
    pages = convert_from_path(head_tail[1], dpi = 500, thread_count = 5) 

    # Counter to store images of each page of PDF to image 
    image_counter = 1

    # Directory 
    directory = str(head_tail[1]).split(".")[0]

    # Parent Directory path 
    parent_dir = x2

    # Path 
    path = os.path.join(parent_dir, directory) 

    # Create the directory 
    os.mkdir(path) 

    # Iterate through all the pages stored above 
    for page in pages: 

        # Declaring filename for each page of PDF as JPG 
        # For each page, filename will be: 
        # PDF page 1 -> page_1.jpg 
        # PDF page 2 -> page_2.jpg 
        # PDF page 3 -> page_3.jpg 
        # .... 
        # PDF page n -> page_n.jpg 
        filename = "page_"+str(image_counter)+".png"

        # Save the image of the page in system 
        page.save(path+'\\'+filename, 'PNG') 

        # Increment the counter to update filename 
        image_counter = image_counter + 1
    
    self.label1 = tk.Label(root, text= "Executed!!!", fg='green')
    self.label1.place(x=30, y=150)

if name == ‘main’:

root = tk.Tk()
run = PDFtoImage(root)
root.mainloop()

Hi Ankita,

So your code works from the Jupyter notebook, but not when frozen
into an exe?

Can you get the script working when run directly from Python at the
command line?

# From Windows command line
python3 myscript.py

My guess is that this will fail with the same error as the exe is
giving. If so, then you can try setting the environment variable

if __name__ == '__main__':
    import os
    os.putenv('XXXX', r'C:\Users\<username>\Poppler\bin')
    root = tk.Tk()
    run = PDFtoImage(root)
    root.mainloop()

where ‘XXXX’ should be replaced by whatever the enviroment variable you
set in Jupyter. If that works as a script, then try re-building the exe
and see it it works now.

Alternatively, check the docs for pyinstaller and see if it gives you
the option to set environment variables when you freeze an exe.