exe file created with pyinstaller not recognizing the external sources
<p>I have created with success an exe file with Pyinstaller. However when I run the exe file and I fill in the path, file &amp; sheetnames in the messagebox that pops up the exe file says that the files that I have mistyped either a filename or sheetname. I obviously types this message myself and therefore my question is: Why does the exe file have troubles finding my files whereas when I do exactly the same in PyCharm there is no trouble running it?</p>

<pre><code> import pandas as pd
import numpy as np
import tkinter as tk
from tkinter import messagebox

def create_file():

try:
    FILEPATH = e0.get()
    w_filename = e1.get()
    x_filename = e2.get()
    y_filename = e3.get()
    z_inventory_filename = e4.get()
    aa_active_filename = e5.get()
    ab_test_filename = e6.get()
    output_filename = e7.get()

w_sheetname = e1_sheet.get()
x_sheetname = e2_sheet.get()
y_sheetname = e3_sheet.get()
z_sheetname = e4_sheet.get()
aa_sheetname = e5_sheet.get()
ab_test_sheetname = e6_sheet.get()
except:
    messagebox.showinfo("Error", "Please fill out all fields.")

try:
    w= pd.read_excel(FILEPATH +"\\"+ w_filename, sheetname=w_sheetname, header=0)
    x = pd.read_excel(FILEPATH +"\\"+ x_filename, sheetname=x_sheetname, header=0)
    y = pd.read_excel(FILEPATH +"\\"+y_filename, sheetname=y_sheetname, header=0)
    z_inventory = pd.read_excel(FILEPATH +"\\"+  z_inventory_filename, sheetname=z_inventory_sheetname, header=0)
    aa_active = pd.read_excel(FILEPATH +"\\"+ aa_active_filename, sheetname=aa_active_sheetname, header=0)
    ab_test_ready = pd.read_excel(FILEPATH +"\\"+ ab_test_filename, sheetname=ab_test_sheetname, header=0)
except:
    messagebox.showinfo("Error", "You have mistyped either a filename or a sheetname.")
</code></pre>

<p>Hope anyone has a specific answer to this.</p>

<p>Thanks,</p>

<p>Jeroen</p>

<p>Define a function to translate path.</p>

<pre><code>import os, sys

def resource_path(relative_path):
    if hasattr(sys, "_MEIPASS"):
        base_path = sys._MEIPASS
    else:
        base_path = os.path.abspath(".")
    return os.path.join(base_path, relative_path)
</code></pre>

<p>Use this function to warp your file path, for example: </p>

<pre><code>bingo_music = resource_path('resources/bingo.wav')
demo_file = resource_path('texts/demo.txt')
</code></pre>

<p>In your <code>.spec</code> file, put a list in <code>exe = EXE()</code>:</p>

<pre><code>[('resources/bingo.wav', r'C:\Users\Administrator\resources\bingo.wav', 'music'),
[('texts/demo.txt', r'C:\Users\Administrator\texts\demo.txt', 'text'),],
</code></pre>

<p>Write every file you use in your project, as a tuple<code>(relative_path, absolute_path, folder_name_in_bundled_app)</code>, the third argument is the name of the folder in the bundled app where your file will be copied into.  Then files will work properly.</p>

python python-3.x exe pyinstaller