{"id":983089,"date":"2024-12-27T07:16:21","date_gmt":"2024-12-26T23:16:21","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/983089.html"},"modified":"2024-12-27T07:16:23","modified_gmt":"2024-12-26T23:16:23","slug":"python%e5%a6%82%e4%bd%95%e5%8d%b8%e8%bd%bd%e7%94%b5%e8%84%91%e7%a8%8b%e5%ba%8f","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/983089.html","title":{"rendered":"python\u5982\u4f55\u5378\u8f7d\u7535\u8111\u7a0b\u5e8f"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24211306\/60e69818-8a79-473c-8e1d-c679e01b3e4c.webp\" alt=\"python\u5982\u4f55\u5378\u8f7d\u7535\u8111\u7a0b\u5e8f\" \/><\/p>\n<p><p> <strong>\u4f7f\u7528Python\u5378\u8f7d\u7535\u8111\u7a0b\u5e8f\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u8c03\u7528\u64cd\u4f5c\u7cfb\u7edf\u547d\u4ee4\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\u7b49\u3002\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\u662f\u5229\u7528Windows\u7684\u5185\u7f6e\u547d\u4ee4<code>wmic<\/code>\u3001\u901a\u8fc7<code>subprocess<\/code>\u6a21\u5757\u6267\u884c\u547d\u4ee4\u3001\u4f7f\u7528<code>pywin32<\/code>\u5e93\u4e0eWindows API\u4ea4\u4e92\u3002<\/strong>\u5728\u8fd9\u91cc\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u901a\u8fc7Python\u6765\u5378\u8f7d\u7535\u8111\u7a0b\u5e8f\uff0c\u5c24\u5176\u662f\u5728Windows\u64cd\u4f5c\u7cfb\u7edf\u4e0a\uff0c\u8fd9\u6d89\u53ca\u5230\u5bf9\u7cfb\u7edf\u547d\u4ee4\u7684\u8c03\u7528\u4ee5\u53ca\u5bf9\u6ce8\u518c\u8868\u7684\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528<code>wmic<\/code>\u547d\u4ee4<\/p>\n<\/p>\n<p><p>\u5728Windows\u4e0a\uff0c<code>wmic<\/code>\uff08Windows Management Instrumentation Command-line\uff09\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u547d\u4ee4\u884c\u5de5\u5177\uff0c\u53ef\u4ee5\u7528\u6765\u7ba1\u7406\u548c\u64cd\u4f5c\u8ba1\u7b97\u673a\u7cfb\u7edf\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7Python\u7684<code>subprocess<\/code>\u6a21\u5757\u6765\u8c03\u7528<code>wmic<\/code>\u547d\u4ee4\u5378\u8f7d\u7a0b\u5e8f\u3002<\/p>\n<\/p>\n<p><h3>1\u3001\u8c03\u7528<code>wmic<\/code>\u547d\u4ee4<\/h3>\n<\/p>\n<p><p><code>wmic<\/code>\u547d\u4ee4\u53ef\u4ee5\u5217\u51fa\u5f53\u524d\u7cfb\u7edf\u4e2d\u5b89\u88c5\u7684\u6240\u6709\u7a0b\u5e8f\uff0c\u5e76\u4e14\u53ef\u4ee5\u901a\u8fc7\u547d\u4ee4\u884c\u53c2\u6570\u6765\u5378\u8f7d\u6307\u5b9a\u7684\u7a0b\u5e8f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import subprocess<\/p>\n<p>def uninstall_program(program_name):<\/p>\n<p>    try:<\/p>\n<p>        # \u4f7f\u7528wmic\u547d\u4ee4\u67e5\u627e\u5e76\u5378\u8f7d\u7a0b\u5e8f<\/p>\n<p>        subprocess.run([&#39;wmic&#39;, &#39;product&#39;, &#39;where&#39;, f&#39;name=&quot;{program_name}&quot;&#39;, &#39;call&#39;, &#39;uninstall&#39;], check=True)<\/p>\n<p>        print(f&#39;{program_name} has been successfully uninstalled.&#39;)<\/p>\n<p>    except subprocess.CalledProcessError as e:<\/p>\n<p>        print(f&#39;An error occurred: {e}&#39;)<\/p>\n<h2><strong>\u793a\u4f8b\u8c03\u7528<\/strong><\/h2>\n<p>uninstall_program(&#39;ProgramName&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2\u3001\u83b7\u53d6\u7a0b\u5e8f\u5217\u8868<\/h3>\n<\/p>\n<p><p>\u5728\u5378\u8f7d\u7a0b\u5e8f\u4e4b\u524d\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u83b7\u53d6\u7cfb\u7edf\u4e2d\u6240\u6709\u5df2\u5b89\u88c5\u7a0b\u5e8f\u7684\u5217\u8868\uff0c\u4ee5\u4fbf\u786e\u8ba4\u7a0b\u5e8f\u7684\u540d\u79f0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def list_installed_programs():<\/p>\n<p>    try:<\/p>\n<p>        # \u8c03\u7528wmic\u547d\u4ee4\u83b7\u53d6\u5df2\u5b89\u88c5\u7a0b\u5e8f\u5217\u8868<\/p>\n<p>        result = subprocess.run([&#39;wmic&#39;, &#39;product&#39;, &#39;get&#39;, &#39;name&#39;], capture_output=True, text=True, check=True)<\/p>\n<p>        programs = result.stdout.splitlines()<\/p>\n<p>        for program in programs:<\/p>\n<p>            print(program)<\/p>\n<p>    except subprocess.CalledProcessError as e:<\/p>\n<p>        print(f&#39;An error occurred: {e}&#39;)<\/p>\n<h2><strong>\u5217\u51fa\u6240\u6709\u5df2\u5b89\u88c5\u7a0b\u5e8f<\/strong><\/h2>\n<p>list_installed_programs()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528<code>pywin32<\/code>\u5e93<\/p>\n<\/p>\n<p><p><code>pywin32<\/code>\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u5e93\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u901a\u8fc7Python\u4e0eWindows API\u8fdb\u884c\u4ea4\u4e92\u3002\u901a\u8fc7\u8be5\u5e93\uff0c\u6211\u4eec\u53ef\u4ee5\u8bbf\u95eeWindows\u6ce8\u518c\u8868\uff0c\u4ee5\u83b7\u53d6\u548c\u64cd\u4f5c\u5df2\u5b89\u88c5\u7a0b\u5e8f\u7684\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><h3>1\u3001\u5b89\u88c5<code>pywin32<\/code><\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u9700\u8981\u786e\u4fdd\u5b89\u88c5\u4e86<code>pywin32<\/code>\u5e93\uff0c\u53ef\u4ee5\u4f7f\u7528<code>pip<\/code>\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pywin32<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2\u3001\u8bbf\u95ee\u6ce8\u518c\u8868<\/h3>\n<\/p>\n<p><p>\u5728Windows\u4e0a\uff0c\u5df2\u5b89\u88c5\u7a0b\u5e8f\u7684\u4fe1\u606f\u901a\u5e38\u5b58\u50a8\u5728\u6ce8\u518c\u8868\u4e2d\uff0c\u901a\u8fc7\u8bbf\u95ee\u6ce8\u518c\u8868\uff0c\u6211\u4eec\u53ef\u4ee5\u83b7\u53d6\u7a0b\u5e8f\u7684\u5378\u8f7d\u547d\u4ee4\u5e76\u6267\u884c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import winreg<\/p>\n<p>import subprocess<\/p>\n<p>def get_uninstall_command(program_name):<\/p>\n<p>    try:<\/p>\n<p>        # \u6253\u5f00\u6ce8\u518c\u8868\u9879<\/p>\n<p>        reg_path = r&#39;SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall&#39;<\/p>\n<p>        reg_key = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, reg_path)<\/p>\n<p>        # \u904d\u5386\u6ce8\u518c\u8868\u9879\uff0c\u67e5\u627e\u6307\u5b9a\u7a0b\u5e8f\u7684\u5378\u8f7d\u547d\u4ee4<\/p>\n<p>        for i in range(0, winreg.QueryInfoKey(reg_key)[0]):<\/p>\n<p>            sub_key_name = winreg.EnumKey(reg_key, i)<\/p>\n<p>            sub_key = winreg.OpenKey(reg_key, sub_key_name)<\/p>\n<p>            try:<\/p>\n<p>                display_name = winreg.QueryValueEx(sub_key, &#39;DisplayName&#39;)[0]<\/p>\n<p>                if display_name == program_name:<\/p>\n<p>                    uninstall_command = winreg.QueryValueEx(sub_key, &#39;UninstallString&#39;)[0]<\/p>\n<p>                    return uninstall_command<\/p>\n<p>            except FileNotFoundError:<\/p>\n<p>                continue<\/p>\n<p>    except Exception as e:<\/p>\n<p>        print(f&#39;An error occurred: {e}&#39;)<\/p>\n<p>def uninstall_program_with_command(program_name):<\/p>\n<p>    uninstall_command = get_uninstall_command(program_name)<\/p>\n<p>    if uninstall_command:<\/p>\n<p>        try:<\/p>\n<p>            subprocess.run(uninstall_command, shell=True, check=True)<\/p>\n<p>            print(f&#39;{program_name} has been successfully uninstalled.&#39;)<\/p>\n<p>        except subprocess.CalledProcessError as e:<\/p>\n<p>            print(f&#39;An error occurred: {e}&#39;)<\/p>\n<p>    else:<\/p>\n<p>        print(f&#39;Uninstall command for {program_name} not found.&#39;)<\/p>\n<h2><strong>\u793a\u4f8b\u8c03\u7528<\/strong><\/h2>\n<p>uninstall_program_with_command(&#39;ProgramName&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001\u6ce8\u610f\u4e8b\u9879<\/p>\n<\/p>\n<p><h3>1\u3001\u6743\u9650\u95ee\u9898<\/h3>\n<\/p>\n<p><p>\u5728\u8c03\u7528\u8fd9\u4e9b\u547d\u4ee4\u65f6\uff0c\u53ef\u80fd\u9700\u8981\u7ba1\u7406\u5458\u6743\u9650\u3002\u5982\u679c\u9047\u5230\u6743\u9650\u95ee\u9898\uff0c\u53ef\u4ee5\u5c1d\u8bd5\u4ee5\u7ba1\u7406\u5458\u8eab\u4efd\u8fd0\u884cPython\u811a\u672c\u3002<\/p>\n<\/p>\n<p><h3>2\u3001\u7a0b\u5e8f\u540d\u79f0\u7684\u51c6\u786e\u6027<\/h3>\n<\/p>\n<p><p>\u7a0b\u5e8f\u540d\u79f0\u5fc5\u987b\u4e0e\u7cfb\u7edf\u4e2d\u5b89\u88c5\u7684\u7a0b\u5e8f\u540d\u79f0\u5b8c\u5168\u4e00\u81f4\uff0c\u5efa\u8bae\u5148\u4f7f\u7528\u83b7\u53d6\u7a0b\u5e8f\u5217\u8868\u7684\u65b9\u6cd5\u786e\u8ba4\u7a0b\u5e8f\u540d\u79f0\u3002<\/p>\n<\/p>\n<p><h3>3\u3001\u517c\u5bb9\u6027<\/h3>\n<\/p>\n<p><p>\u4e0a\u8ff0\u65b9\u6cd5\u4e3b\u8981\u9488\u5bf9Windows\u7cfb\u7edf\u3002\u5bf9\u4e8e\u5176\u4ed6\u64cd\u4f5c\u7cfb\u7edf\uff0c\u9700\u8981\u4f7f\u7528\u76f8\u5e94\u7684\u5305\u7ba1\u7406\u5668\u6216\u547d\u4ee4\u884c\u5de5\u5177\u8fdb\u884c\u5378\u8f7d\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u65b9\u6cd5\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528Python\u811a\u672c\u6765\u81ea\u52a8\u5316\u5378\u8f7d\u7535\u8111\u7a0b\u5e8f\u7684\u8fc7\u7a0b\u3002\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u4ec5\u53ef\u4ee5\u63d0\u9ad8\u6548\u7387\uff0c\u8fd8\u53ef\u4ee5\u5e94\u7528\u4e8e\u6279\u91cf\u5378\u8f7d\u573a\u666f\u4e2d\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u811a\u672c\u6765\u5378\u8f7d\u7279\u5b9a\u7684\u8f6f\u4ef6\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528Python\u7684subprocess\u6a21\u5757\u6765\u8c03\u7528\u7cfb\u7edf\u547d\u4ee4\uff0c\u6267\u884c\u5378\u8f7d\u7a0b\u5e8f\u3002\u4f8b\u5982\uff0c\u5728Windows\u7cfb\u7edf\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u8c03\u7528\u201cwmic\u201d\u547d\u4ee4\u6765\u5378\u8f7d\u8f6f\u4ef6\u3002\u4f60\u9700\u8981\u77e5\u9053\u8f6f\u4ef6\u7684\u540d\u79f0\u6216\u5176\u5b89\u88c5\u7684\u552f\u4e00\u6807\u8bc6\u7b26\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">import subprocess\n\nsoftware_name = &quot;SoftwareName&quot;  # \u66ff\u6362\u4e3a\u8981\u5378\u8f7d\u7684\u7a0b\u5e8f\u540d\u79f0\nsubprocess.run(f&quot;wmic product where name=&#39;{software_name}&#39; call uninstall&quot;, shell=True)\n<\/code><\/pre>\n<p><strong>\u5728Python\u4e2d\u662f\u5426\u53ef\u4ee5\u67e5\u770b\u5df2\u5b89\u88c5\u7a0b\u5e8f\u7684\u5217\u8868\uff1f<\/strong><br \/>\u662f\u7684\uff0c\u53ef\u4ee5\u901a\u8fc7\u8c03\u7528\u7cfb\u7edf\u547d\u4ee4\u5728Python\u4e2d\u83b7\u53d6\u5df2\u5b89\u88c5\u7a0b\u5e8f\u7684\u5217\u8868\u3002\u5728Windows\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u201cwmic product get name\u201d\u547d\u4ee4\uff0c\u7ed3\u5408subprocess\u6a21\u5757\u6765\u83b7\u53d6\u5e76\u6253\u5370\u5df2\u5b89\u88c5\u7a0b\u5e8f\u7684\u540d\u79f0\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">import subprocess\n\ninstalled_programs = subprocess.run(&quot;wmic product get name&quot;, shell=True, capture_output=True, text=True)\nprint(installed_programs.stdout)\n<\/code><\/pre>\n<p><strong>\u4f7f\u7528Python\u5378\u8f7d\u7a0b\u5e8f\u65f6\u4f1a\u9047\u5230\u54ea\u4e9b\u5e38\u89c1\u95ee\u9898\uff1f<\/strong><br \/>\u5728\u4f7f\u7528Python\u5378\u8f7d\u7a0b\u5e8f\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u6743\u9650\u4e0d\u8db3\u7684\u95ee\u9898\uff0c\u7279\u522b\u662f\u5728Windows\u7cfb\u7edf\u4e2d\uff0c\u67d0\u4e9b\u7a0b\u5e8f\u53ef\u80fd\u9700\u8981\u7ba1\u7406\u5458\u6743\u9650\u624d\u80fd\u5378\u8f7d\u3002\u786e\u4fdd\u4ee5\u7ba1\u7406\u5458\u8eab\u4efd\u8fd0\u884cPython\u811a\u672c\uff0c\u6216\u4f7f\u7528sudo\u5728Linux\u7cfb\u7edf\u4e2d\u8fd0\u884c\u811a\u672c\u3002\u6b64\u5916\uff0c\u786e\u4fdd\u4f60\u8f93\u5165\u7684\u8f6f\u4ef6\u540d\u79f0\u51c6\u786e\u65e0\u8bef\uff0c\u4ee5\u907f\u514d\u5378\u8f7d\u5931\u8d25\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4f7f\u7528Python\u5378\u8f7d\u7535\u8111\u7a0b\u5e8f\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u8c03\u7528\u64cd\u4f5c\u7cfb\u7edf\u547d\u4ee4\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\u7b49\u3002\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\u662f\u5229\u7528Window [&hellip;]","protected":false},"author":3,"featured_media":983097,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/983089"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=983089"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/983089\/revisions"}],"predecessor-version":[{"id":983098,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/983089\/revisions\/983098"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/983097"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=983089"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=983089"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=983089"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}