Search code examples
pythonsqliteexeexecutablearchiving

Python programme not working in executable file - archiving,sqlite3, tkinter


I just finished my first Python programme, which basically archives user-specified files. There is a GUI made with tkinter and sqlite3. Finally I got it to work fine in Python. Then I made it into an .exe file using pyinstaller, and the key component (the archiving) is just not happening.

Within the Archive() function, I created pop up windows to show me the paths and they all seem fine, so the thread is working, but the actual archiving is not happening. Here is a small part of the code, which I think is where the error lies.

def Archiver(input_file, output_dir, archive_int):
    while True:
        global sched_var
        time.sleep(archive_int*60)                       
        input_name = os.path.basename(input_file)
        main_dir = os.path.dirname(input_file)
        if not os.path.exists(main_dir + '/temp'): 
                os.mkdir(main_dir + '/temp')
        ## copy file to temporary directory
        shutil.copy(input_file, os.path.join(main_dir, 'temp'))
        ## hold all the paths in variables to check they are okay
        tempfiledir = os.path.join(main_dir + '/temp/' + input_name)
        archfile = input_file + "_" + time.strftime("%H%M_%d%m%Y")+'.rar'
        outputarch = output_dir + '/' + input_name + "_" + time.strftime("%H%M_%d%m%Y")+'.rar'
        messagebox.showinfo(title = 'tempfiledir', message = tempfiledir)    ## all these seem okay 
        messagebox.showinfo(title = 'archfile', message = archfile)
        messagebox.showinfo(title = 'outputarch', message = outputarch)
        patoolib.create_archive(archfile,(tempfiledir,),)          ## from here - nothing executed  
        messagebox.showinfo(title = 'Archive', message = 'Archive created')
        shutil.move(archfile, outputarch)
        messagebox.showinfo(title = 'Moved', message = 'Archive moved')
        if sched_var == False:
            break 

As I said, it works perfect when run in Python, is there something with os.path that is different once the file is an .exe file?


Solution

  • Yes, basically the executable gets extracted to a temporary directory and then runs there. The path to that directory is saved in a system variable called _MEIPASS. You can look here for how to retrieve that path and create an absolute path with the temp folder location.