You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

为何msiexec.exe执行时提示"系统找不到指定路径"?Python调用故障求助

问题:subprocess调用msiexec.exe返回错误1639,提示“系统找不到指定路径”

在Windows 10 21H2系统中使用Python 3.9.7编写脚本,通过subprocess模块调用各类程序时,其他程序均能正常执行,但msiexec.exe相关的安装命令始终失败,返回代码1639,报错信息为"系统找不到指定路径"。单独在CMD窗口运行命令结果一致,相关代码如下:

import os
import subprocess

def run(name, para):    
    try:
        print(f"Running the following:\n\tProgram: {name}\n\tCommand: {para}\n")
        status =  subprocess.run([name, para], capture_output=True, shell=True, text=True)

    finally:
        return status

class Settings:
    def __init__(self):
        self.timezone = 'Canada Central Standard Time'
        #self.installationsList = [("notepad.exe", "text.txt"), ('calc.exe', '')]
        
        self.installationsList = [('notepad.exe', 'text.txt'),
        ('msiexec.exe', '/i googlechromestandaloneenterprise64.msi /qn /norestart /lv chrome.log'),
        ('Dell-Command-Update-Windows-Universal-Application_601KT_WIN_4.5.0_A00_01.exe /s /e=c:\Temp', ''),
        ('PlantronicsHubInstaller.EXE /quiet /norestart', ''),
        ('msiexec.exe /I 7z2201-x64.msi /qn', ''),
        ('powershell.exe', ' -executionpolicy bypass -file Remove_Mail_Calendar.ps1')]
        
    
    def __str__(self):
        return self.timezone

    def LaunchInstallations(self):
        for name,para in self.installationsList:
            print(run(name, para))

    def SetTimeZone(self):
        run(f"powershell.exe Set-TimeZone -Id '{self.timezone}'", '')
        run('W32tm /resync /force', '')


if __name__ == "__main__":
    print(os.curdir)
    settings = Settings()
    print(settings)
    settings.SetTimeZone() # Setting the local timezone
    settings.LaunchInstallations() # Lauching the list of common installed applications.
    print("Closing Program!")

问题原因与解决方案

核心原因

  1. 参数传递格式错误:当使用shell=True时,你传入的[name, para]列表会被Python拼接成类似msiexec.exe "/i googlechromestandaloneenterprise64.msi /qn /norestart /lv chrome.log"的命令——msiexec会把整个带空格的para字符串当作单个参数,而非一组独立的命令行选项,自然无法识别其中的msi文件路径。
  2. 命令拆分错误:列表中的('msiexec.exe /I 7z2201-x64.msi /qn', '')项把程序名和参数全部塞进了name字段,para为空,这种拆分方式会导致shell解析命令时出错。
  3. 路径转义问题:c:\Temp中的\T会被Python当作转义字符处理,实际传递给程序的路径会变成c:Temp,导致路径无效。

修复步骤

1. 统一命令传递方式

当shell=True时,直接传递完整的命令字符串而非拆分后的列表,避免参数打包错误。修改run函数为接收单条命令字符串。

2. 修正列表中的错误项

  • 把('msiexec.exe /I 7z2201-x64.msi /qn', '')改为完整命令字符串格式
  • 把c:\Temp改为c:\\Temp,避免转义字符干扰

3. 确保msi文件路径正确

如果msi文件不在脚本当前工作目录,使用绝对路径(比如os.path.abspath("googlechromestandaloneenterprise64.msi"))来指定文件位置,避免相对路径识别失败。

修改后的完整代码

import os
import subprocess

def run(cmd):    
    try:
        print(f"Running command:\n\t{cmd}\n")
        status = subprocess.run(cmd, capture_output=True, shell=True, text=True)
    finally:
        return status

class Settings:
    def __init__(self):
        self.timezone = 'Canada Central Standard Time'
        
        self.installationsList = [
            'notepad.exe text.txt',
            f'msiexec.exe /i {os.path.abspath("googlechromestandaloneenterprise64.msi")} /qn /norestart /lv chrome.log',
            'Dell-Command-Update-Windows-Universal-Application_601KT_WIN_4.5.0_A00_01.exe /s /e=c:\\Temp',
            'PlantronicsHubInstaller.EXE /quiet /norestart',
            f'msiexec.exe /I {os.path.abspath("7z2201-x64.msi")} /qn',
            'powershell.exe -executionpolicy bypass -file Remove_Mail_Calendar.ps1'
        ]
        
    
    def __str__(self):
        return self.timezone

    def LaunchInstallations(self):
        for cmd in self.installationsList:
            print(run(cmd))

    def SetTimeZone(self):
        run(f"powershell.exe Set-TimeZone -Id '{self.timezone}'")
        run('W32tm /resync /force')


if __name__ == "__main__":
    print(os.curdir)
    settings = Settings()
    print(settings)
    settings.SetTimeZone() # Setting the local timezone
    settings.LaunchInstallations() # Launching the list of common installed applications.
    print("Closing Program!")

内容的提问来源于stack exchange,提问作者Tim Neufeld

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.06 00:45:49