M PAUL
M PAUL

Reputation: 1248

Editing a pyinstaller .exe file

I managed to create an executable file in Windows, out of a Python script using pyinstaller. I tried pyinstaller myscript.py and the build and dist folders were created, along with the .spec file

However, at a later time, I made changes to the underlying code. So what I need to do is recompile and my application works again.

But, is there a way in which I can edit the already existing application? Or do I always have to recompile after making a change?

Also, what is the purpose of the .spec file?

Upvotes: 7

Views: 5782

Answers (2)

user25516327
user25516327

Reputation:

To make an editable EXE, consider making an additional file "dummy_python_file.py" that:

  1. Reads the file containing actual code to be executed.(say from "actual_code_file.py")
  2. Uses exec function (exec statement incase of python2) to execute the code of the "actual_code_file.py" (Make sure to add thr working directory of "actual_code_file.py" in in sys.path)

Now, make an executable of this "dummy_python_file.py" instead of "actual_code_file.py" using pyinstaller as usual. This newly created "dummy_python_file.exe" will read the code from your "actual_code_file.py" every-time you execute the application. This way you won't need to recompile the updated file repeatedly.

Example:

dummy_python_file.py

# dummy_python_file.py

# python v3.12.3

# This file has to be converted to ".exe"

import os, sys

# Fetches the directory of executable "dummy_python_file.exe"
# at the moment when "dummy_python_file.exe" is ran.
# This is done so as to find the "actual_code_file.py" relatively
sys.path.append(os.getcwd())

# Opens and reads the "actual_code_file.py"
exceutable_code_str = ""
try:
    with open("./actual_code_file.py") as _code:
        executable_code_str = _code.read()
except FileNotFoundError as err:
    print(err) #In python 2: print err
    input("") ## Will avoid console window from abruptly closing after displaying error.

# Executes the code string.
exec(executable_code_str)

# In python 2: exec executable_code_str

Say our "actual_code_file.py" has some code as follows before making the ".exe" file:

# actual_code_file.py

# python v3.12.3

# This file can an be edited even after making "dummy_python_file.exe"

print("Hello World!")

input("") ## Will avoid console window from abruptly closing after displaying error.

Then, ... ( short gif uploaded here ): Editable EXE.gif

<img src="https://archive.org/download/editable-exe/Editable%20EXE.gif" alt="Editable EXE.gif"  width="550" />

Ideally it should work for nested cases too. Just make sure that all the libraries were properly added during making of ".exe" file from pyinstaller.

Upvotes: 0

Fomalhaut
Fomalhaut

Reputation: 9795

Spec-file is needed to keep some options for pyinstaller to build your project such as hidden imports, attached data files, the name of output exe-file, etc. It is always created by using pyinstaller first time. Next time if you want to build your changed project use this command specifying the spec-file:

$ pyinstaller myscript.spec

For more information about spec-files read documentation: https://pyinstaller.readthedocs.io/en/stable/spec-files.html

Upvotes: 4

Related Questions