I have converted my script of '.py' to '.exe' using pyinstaller and auto-py-to-exe. Both can produce the '.exe' but there is some function where user need to select the files and chose which sheet they want to use. This sheet is not displayed after user select the excel files.

I already try import os but not working. Also, there is no error with the code I think because before conversion, the script works fine and i can see the sheet options after user selected the excel file but not after conversion. Do i need to add specific code or any materials for pyinstaller to read or find my user selection file? Here are some of my code as a reference:

def resource_path(relative_path):
""" Get absolute path to resource, works for dev and for PyInstaller """
try:
    # PyInstaller creates a temp folder and stores path in _MEIPASS
    base_path = sys._MEIPASS
except Exception:
    base_path = os.path.abspath(".")

return os.path.join(base_path, relative_path)

Function to load selected sheet into the DataFrame and populate the ComboBox

def load_selected_sheet(dataframe_name, sheet_combobox):
global file_path
file_path = filedialog.askopenfilename(filetypes=[("Excel files", "*.xlsx *.xls")])
if file_path:
    file_path = resource_path(file_path)
    try:
        excel_file = pd.ExcelFile(file_path)
        sheet_names = excel_file.sheet_names
        sheet_combobox["values"] = sheet_names  
        sheet_combobox.config(state="readonly")  # Enable the ComboBox
        print(f"File '{file_path}' selected.")
    except Exception as e:
        print(f"Error loading data: {str(e)}")

Function to save the selected sheet to the corresponding DataFrame

def save_selected_sheet(dataframe_name, selected_sheet_name):
global file_path
if selected_sheet_name:
    try:
        excel_file = pd.ExcelFile(file_path)
        df = pd.read_excel(excel_file, sheet_name=selected_sheet_name)
        
        globals()[dataframe_name] = df  # Update the global DataFrame

        print(f"Saved selected sheet to {dataframe_name}.")

        #Display message
        messagebox.showinfo("Success",f"Sheet saved to {dataframe_name} successfully.")

My question, is it because of the codes or packages from pyinstaller? I have run it using command prompt with '.py' format and it is working but not when converting it to '.exe'. Can you help me? NOTE: resource path is an addition function to fix this problem but not working. I don't have problem with the codes even without the resource part function, it works well.

2

There are 2 best solutions below

0
On BEST ANSWER

This is due to the pyinstaller package. I have fixed this by using the following prompts:

  • uninstall using pip uninstall pyinstaller
  • reinstall using pip install --ignore-installed pyinstaller.
1
On

I'm not too familiar with the Tkinter stuff, however I would assume this to be a path related issue.

I believe this is an issue with the sys._MEIPATH vs os.path.abspath('.'). Using sys._MEIPATH allows you to find the pyinstaller temp folder, which is for project specific files, however you are loading a user specified file, which is not necessarily part of the project itself. This means you probably just want to use the regular path to the excel file instead of calling resource_path(file_path).

I would also suggest a few other refactors for readability which I've made below (my own preference, watch https://youtu.be/CFRhGnuXG-4)

Please read the comments in this code to understand what I've done and don't just copy/paste it

def load_selected_sheet(dataframe_name, sheet_combobox):
    # I would try to avoid global variables at all costs,
    # Maybe see if you can somehow change this
    global file_path
    file_path = filedialog.askopenfilename(filetypes=[("Excel files", "*.xlsx *.xls")])

    if not file_path:
        return # maybe raise error here instead or something

    # The following line probably isn't needed and is likely causing your error
    #file_path = resource_path(file_path)
    try:
        excel_file = pd.ExcelFile(file_path)
        sheet_names = excel_file.sheet_names
        sheet_combobox["values"] = sheet_names  
        sheet_combobox.config(state="readonly")  # Enable the ComboBox
        print(f"File '{file_path}' selected.")
    except Exception as e:
        print(f"Error loading data: {str(e)}")

def save_selected_sheet(dataframe_name, selected_sheet_name):
    # See comments on load_selected_sheet
    global file_path
    if not selected_sheet_name:
        return

    # Empty try statement, I assume there is error handling code
    # which hasn't been written yet. If not just delete this try statement
    try:
        excel_file = pd.ExcelFile(file_path)
        df = pd.read_excel(excel_file, sheet_name=selected_sheet_name)
        
        globals()[dataframe_name] = df  # Update the global DataFrame

        print(f"Saved selected sheet to {dataframe_name}.")

        #Display message
        messagebox.showinfo("Success",f"Sheet saved to {dataframe_name} successfully.")

If this doesn't work, try using file_path = os.path.abspath(file_path) instead of file_path = resource_path(file_path), I imagine it should work fine without this, but I'm not familiar with the tools you're using