How can I install an MSI using python's subprocess, passing an install directory containing a space?

1.5k views Asked by At

When I try to run an MSI using a Python command of the form similar to

subprocess.call(["msiexec.exe", "/i", "myinstaller.msi", "/log", "myinstalllog.log", "INSTALLDIR=\"C:\Program Files\InstallDirectory\""])

it fails to install and instead I just get the Windows Installer popup window explaining the available command line options. It works if I use a different INSTALLDIR argument containing no spaces (and without the escaped quotes). I can also run the msi from os.system, but this is unsuitable for my intended purpose as I need Python to wait until the installation is complete.

1

There are 1 answers

0
James Kent On

after re reading what you'd put i thing the problem is not actually your quotes, but the slash characters in the string acting as escape characters when they shouldn't be, what i did for testing was create a parent script:

#!python3

import subprocess

if __name__ == "__main__":
    subprocess.call(["py.exe", "child.py", "/i", "myinstaller.msi", "/log", "myinstalllog.log", "INSTALLDIR=\"C:\\Program Files\\InstallDirectory\""])

and a child script:

#!python3

import sys
print("Argument count: %i" % len(sys.argv))
for item in sys.argv:
    print(item)

thing = input("Press return to exit")

notice that in the parent script i have changed:

"INSTALLDIR=\"C:\Program Files\InstallDirectory\""

to:

"INSTALLDIR=\"C:\\Program Files\\InstallDirectory\""

this way the slashes are definitely slashes and not escapes.