Search code examples
pythonpython-3.xurllib

Download and Install a .exe File using Python


I wanna create an auto-updater for my project which downloads and installs a file from the internet. I have done how I can download the file to my desired location with a desired name. The place where I am stuck is how I can install the file.

The code so far I have written:

from urllib.request import urlretrieve
import getpass

url = 'https://sourceforge.net/projects/artigence/files/latest/download'

print('File Downloading')

usrname = getpass.getuser()
destination = f'C:\\Users\\{usrname}\\Downloads\\download.exe'

download = urlretrieve(url, destination)

print('File downloaded')

And the file is downloaded to my downloads folder. Now, how can I install the .exe file using python?


Solution

  • You will need to use the subprocess module to execute the .exe files.

    import subprocess
    
    cmd = "{download location} batch.exe"
    
    returned_value = subprocess.call(cmd, shell=True)  # returns the exit code in unix
    print('returned value:', returned_value)
    

    refer

    I strongly suggest not to use pyautogui for this.