How can I replace an executable created by PyInstaller from within the code?

Viewed 24

I'm trying to automatically update my software when a new version is released. It can check for a new version and download it in a background thread with no problem. It then has two files next to each other - the original executable (currently running; created with PyInstaller), and the new executable binary blob.

What I want to do is os.rename('update-temp.dat', sys.executable) (sys.executable points to the compiled program when using PyInstaller, not the Python interpreter). This works fine on Linux, but not on Windows.

On Windows, just using os.rename() gives a FileExistsError, and when I try to os.remove(sys.executable) first, I get PermissionError: [WinError 5] Access is denied. Using shutil.move() just has the same problem.

Both work fine on Linux.

How can I replace the currently running, PyInstaller-generated executable from within my Python code?

0 Answers
Related