Create a Self-Updating Python Desktop App: Detect, Download, and Replace
This guide explains how to build a Python desktop GUI application with Tkinter, PyQt5, or wxPython, package it using tools like PyInstaller, and implement a robust update mechanism that detects new versions on a remote server, downloads the update, and seamlessly replaces the old files.
Desktop GUI Options
Python provides several libraries for creating cross‑platform desktop graphical user interfaces, including the built‑in Tkinter, the powerful PyQt5 and PySide2, as well as wxPython. These modules enable developers to write GUI programs that run on Windows, macOS, and Linux.
Packaging Tools
To distribute a desktop application, third‑party packaging tools such as PyInstaller, Nuitka, and cx_Freeze can bundle the Python code and its dependencies into a single executable file (e.g., a Windows .exe) or a folder containing all required files.
Why an Update Mechanism Is Needed
Unlike web applications, desktop programs are distributed offline. When a new version is released, the developer must re‑package the program, host the new executable, and ask users to download and install the update manually. Without a proper update flow, users may continue using outdated software.
Update Detection
The application should query a remote update server at startup (usually in a background thread) to determine whether a newer version is available.
Downloading Updates
If an update exists, the program can download it in two ways: directly from the main application or by invoking a separate updater program. The latter is preferred because it allows the updater to replace the running executable.
Download using the main program (cannot overwrite itself).
Launch a dedicated updater executable that handles the download and replacement.
The updater can fetch the update package with the requests library. Example code:
def work(self):
# Create a temporary file for the update package
temp_file = os.path.join(self.download_path, 'update.zip')
logger.info("Download temporary file path: {}".format(temp_file))
with open(temp_file, 'wb') as self.fileobj:
# Stream the update file from the server
f = requests.get(self.download_url, stream=True)
offset = 0
for chunk in f.iter_content(chunk_size=self.buffer):
if not chunk:
break
self.fileobj.seek(offset)
self.fileobj.write(chunk)
offset += len(chunk)
progress = offset / int(self.filesize) * 100
self.download_proess_signal.emit(int(progress))Extracting and Replacing Files
After the new version is downloaded, the updater terminates the running main process, extracts the update archive into the original installation directory, and cleans up temporary files.
def work(self):
# 1. Kill the main application process
for proc in psutil.process_iter():
if proc.name() == 'xxx.exe':
proc.kill()
# 2. Get the current program path
current_path = os.path.dirname(os.path.abspath(__file__))
logger.info("Current program path: {}".format(current_path))
# 3. Extract the update zip to the program directory
update_file_path = os.path.join(self.download_temp_path, 'update.zip')
shutil.unpack_archive(filename=update_file_path, extract_dir=current_path)
# 4. Remove temporary files
os.remove(update_file_path)
shutil.rmtree(self.download_temp_path)By following these steps, a Python‑based desktop GUI application can automatically detect updates, download the new version, and replace the old files without requiring manual user intervention.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Python Crawling & Data Mining
Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
