Aviv Kaplan
Aviv Kaplan

Reputation: 59

After compiling a python script to EXE the Archive method doesn't work

I have a python script that I compiled to an EXE, one of the purposes of it is to extract a 7z file and save it to a destination. If I'm running it from PyCharm everything works great, this is the code:

    def delete_old_version_rename_new(self):
        winutils.delete(self.old_version)
        print("Extracting...")
        Archive(f"{self.new_version}_0.7z").extractall(f"{self.destination}")
        print("Finished")
        os.rename(self.new_version, self.new_name)

I used pyinstaller to compile the program and used to command pyinstaller --onefile --paths {path to site packages} main.py

Thanks!

Upvotes: 1

Views: 527

Answers (2)

Aviv Kaplan
Aviv Kaplan

Reputation: 59

Eventually i just used a 7z command line i took from https://superuser.com/questions/95902/7-zip-and-unzipping-from-command-line and used os.system() to initialize it. Since my program is command line based it worked even better since its providing a status on the extraction process. Only downside is i have to move the 7z.exe to the directory of my program.

Upvotes: 0

Kemp
Kemp

Reputation: 3649

Single-file executables self-extract to a temporary folder and run from there, so any relative paths will be relative to that folder not the location of executable you originally ran.

My solution is a code snippet such as this:

if getattr(sys, 'frozen', False):
    app_path = os.path.dirname(sys.executable)
else:
    app_path = os.path.dirname(os.path.abspath(__file__))

which gives you the location of the executable in the single-file executable case or the location of the script in the case of running from source.

See the PyInstaller documentation here.

Upvotes: 1

Related Questions