{"id":18238,"date":"2024-12-10T14:59:53","date_gmt":"2024-12-10T06:59:53","guid":{"rendered":"https:\/\/blog.iyatt.com\/?p=18238"},"modified":"2025-02-04T16:52:52","modified_gmt":"2025-02-04T08:52:52","slug":"python-%e7%a8%8b%e5%ba%8f%e6%a3%80%e6%9f%a5%e6%98%af%e5%90%a6%e5%85%b7%e6%9c%89%e7%ae%a1%e7%90%86%e5%91%98%e6%9d%83%e9%99%90%ef%bc%88%e4%b8%8d%e6%98%af%e8%af%b7%e6%b1%82%e6%9d%83%e9%99%90%e9%87%8d","status":"publish","type":"post","link":"https:\/\/blog.iyatt.com\/?p=18238","title":{"rendered":"Windows \u4e0b Python \u7a0b\u5e8f\u63d0\u6743\u7ba1\u7406\u5458"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_82_2 ez-toc-wrap-center counter-hierarchy ez-toc-counter ez-toc-light-blue ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title ez-toc-toggle\" style=\"cursor:pointer\">\u76ee\u5f55<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/blog.iyatt.com\/?p=18238\/#%E6%B5%8B%E8%AF%95%E7%8E%AF%E5%A2%83\" >\u6d4b\u8bd5\u73af\u5883<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/blog.iyatt.com\/?p=18238\/#%E5%AE%9E%E7%8E%B0\" >\u5b9e\u73b0<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/blog.iyatt.com\/?p=18238\/#%E6%B3%A8%E6%84%8F\" >\u6ce8\u610f<\/a><\/li><\/ul><\/nav><\/div>\n<h1><span class=\"ez-toc-section\" id=\"%E6%B5%8B%E8%AF%95%E7%8E%AF%E5%A2%83\"><\/span>\u6d4b\u8bd5\u73af\u5883<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<p>Windows 11 24H2<br \/>\nPython 3.13.1<\/p>\n<h1><span class=\"ez-toc-section\" id=\"%E5%AE%9E%E7%8E%B0\"><\/span>\u5b9e\u73b0<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<pre><code class=\"language-python\">import sys\nimport ctypes\n\ndef getPackagedStatus():\n    &quot;&quot;&quot;\n    \u83b7\u53d6\u6253\u5305\u72b6\u6001\n\n    Returns:\n        bool: \u662f\u5426\u4e3a\u6253\u5305\u72b6\u6001\n    &quot;&quot;&quot;\n    return getattr(sys, &#039;frozen&#039;, False)\n\ndef quotingArgs(args: list[str]) -&gt; str:\n    &quot;&quot;&quot;\n    \u4e3a\u53c2\u6570\u6dfb\u52a0\u53cc\u5f15\u53f7\u5e76\u62fc\u63a5\u4e3a\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u4fdd\u8bc1\u53c2\u6570\u53ef\u4ee5\u6b63\u786e\u5904\u7406\n\n    Params:\n        args (list[str]): \u53c2\u6570\u5217\u8868\n\n    Returns:\n        str: \u62fc\u63a5\u540e\u7684\u5b57\u7b26\u4e32\n    &quot;&quot;&quot;\n    return &#039; &#039;.join(f&#039; &quot;{arg}&quot;&#039; for arg in args)\n\ndef checkAdmin():\n    &quot;&quot;&quot;\n    \u68c0\u67e5\u662f\u5426\u662f\u7ba1\u7406\u5458\n\n    Returns:\n        bool: \u662f\u5426\u662f\u7ba1\u7406\u5458\n    &quot;&quot;&quot;\n    try:\n        return ctypes.windll.shell32.IsUserAnAdmin()\n    except:\n        return False\n\ndef runAsAdmin() -&gt; tuple[bool, str]:\n    &quot;&quot;&quot;\n    \u4ee5\u7ba1\u7406\u5458\u8eab\u4efd\n\n    Returns:\n        (\u72b6\u6001, \u6d88\u606f)\uff1a \u4e09\u79cd\u72b6\u6001\uff1aNone\uff1a\u5df2\u7ecf\u662f\u7ba1\u7406\u5458\u8eab\u4efd\uff1bTrue \u4ee5\u7ba1\u7406\u5458\u8eab\u4efd\u91cd\u65b0\u8fd0\u884c\u6210\u529f\uff1bFalse \u4ee5\u7ba1\u7406\u5458\u8eab\u4efd\u91cd\u65b0\u8fd0\u884c\u5931\u8d25\n    &quot;&quot;&quot;\n    if checkAdmin():\n        return None, &#039;\u5df2\u7ecf\u662f\u7ba1\u7406\u5458&#039;\n\n    try:\n        if getPackagedStatus():\n            params = quotingArgs(sys.argv[1:])\n        else:\n            params = quotingArgs(sys.argv)\n        result = ctypes.windll.shell32.ShellExecuteW(None, &quot;runas&quot;, sys.executable, params, None, 1)\n        if result &gt; 32:\n            return True, &#039;\u4ee5\u7ba1\u7406\u5458\u8fd0\u884c\u6210\u529f&#039;\n        else:\n            match result:\n                case 5:\n                    error = &#039;\u8bbf\u95ee\u88ab\u62d2\u7edd&#039;\n                case _:\n                    error = &#039;&#039;\n            return False, f&#039;\u4ee5\u7ba1\u7406\u5458\u8eab\u4efd\u8fd0\u884c\u5931\u8d25\uff0c\u9519\u8bef\u4ee3\u7801\uff1a{result} {error}&#039;\n    except Exception as e:\n        return False, f&#039;\u4ee5\u7ba1\u7406\u5458\u8eab\u4efd\u8fd0\u884c\u65f6\u53d1\u751f\u9519\u8bef\uff1a{e}&#039;\n\nfrom commontools import CommonTools\n\ndef main():\n    status, info = CommonTools.runAsAdmin()\n    if status is None:\n        ###################################\n        # \u628a\u9700\u8981\u4ee5\u7ba1\u7406\u5458\u8eab\u4efd\u8fd0\u884c\u7684\u4ee3\u7801\u653e\u5728\u8fd9\u91cc\n        ###################################\n        info += &#039; \u5de5\u4f5c\u903b\u8f91\u653e\u5728\u8fd9\u91cc&#039;\n        print(info)\n\n        import time\n        time.sleep(3)\n        print(&#039;\u5230\u6b64\u4e00\u6e38&#039;)\n        import os\n        os.system(&#039;pause&#039;)\n    elif status:\n        info += &#039; \u4ee5\u7ba1\u7406\u5458\u8eab\u4efd\u8fd0\u884c\u4e86\u4e00\u4e2a\u65b0\u7a0b\u5e8f\uff0c\u6211\u7684\u5bff\u547d\u5230\u6b64\u7ed3\u675f\u4e86&#039;\n        print(info)\n    else:\n        print(info)\n\nif __name__ == &#039;__main__&#039;:\n    main()<\/code><\/pre>\n<p>(1) \u4ee3\u7801\u8fd0\u884c<br \/>\n\u2460 \u4ee5\u7ba1\u7406\u5458\u8eab\u4efd\u8fd0\u884c<br \/>\n<img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2024\/12\/image-1738398935193.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 491px; --smush-placeholder-aspect-ratio: 491\/76;\" \/><br \/>\n\u2461 \u4ee5\u975e\u7ba1\u7406\u5458\u8eab\u4efd\u8fd0\u884c\uff0c\u4f46\u62d2\u7edd\u6388\u6743<br \/>\n<img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2024\/12\/image-1738398995546.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 524px; --smush-placeholder-aspect-ratio: 524\/49;\" \/><br \/>\n\u2462 \u4ee5\u975e\u7ba1\u7406\u5458\u8eab\u4efd\u8fd0\u884c\uff0c\u540c\u610f\u6388\u6743<br \/>\n\u4e0a\u9762\u662f\u975e\u7ba1\u7406\u5458\uff0c\u5b83\u8bf7\u6c42\u4ee5\u7ba1\u7406\u5458\u91cd\u65b0\u8fd0\u884c\u4e86\u81ea\u5df1\uff0c\u4e0b\u9762\u65b0\u8fd0\u884c\u7684\u7a0b\u5e8f\u5c31\u5df2\u7ecf\u662f\u7ba1\u7406\u5458\u8eab\u4efd\u4e86<br \/>\n<img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2024\/12\/image-1738399068625.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 608px; --smush-placeholder-aspect-ratio: 608\/160;\" \/><\/p>\n<p>\uff082\uff09\u6253\u5305\u540e\u8fd0\u884c<br \/>\n\u2460 \u4ee5\u7ba1\u7406\u5458\u8eab\u4efd\u8fd0\u884c<br \/>\n<img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2024\/12\/image-1738399252539.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 469px; --smush-placeholder-aspect-ratio: 469\/68;\" \/><br \/>\n\u2461 \u4ee5\u975e\u7ba1\u7406\u5458\u8eab\u4efd\u8fd0\u884c\uff0c\u4f46\u62d2\u7edd\u6388\u6743<br \/>\n<img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2024\/12\/image-1738399269130.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 555px; --smush-placeholder-aspect-ratio: 555\/66;\" \/><br \/>\n\u2462 \u4ee5\u975e\u7ba1\u7406\u5458\u8eab\u4efd\u8fd0\u884c\uff0c\u540c\u610f\u6388\u6743<br \/>\n<img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2024\/12\/image-1738399297699.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 602px; --smush-placeholder-aspect-ratio: 602\/158;\" \/><\/p>\n<h1><span class=\"ez-toc-section\" id=\"%E6%B3%A8%E6%84%8F\"><\/span>\u6ce8\u610f<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<p>\u4f7f\u7528\u8c03\u8bd5\u8fd0\u884c\u53ef\u80fd\u4f1a\u51fa\u73b0\u95ea\u9000\u7684\u73b0\u8c61\u3002<br \/>\n\u6bd4\u5982\u6211\u7528 VScode \u8c03\u8bd5\u7a0b\u5e8f\uff0cVScode \u662f\u975e\u7ba1\u7406\u5458\u8fd0\u884c\u7684\uff0c\u4f7f\u7528\u4e0a\u9762\u4ee3\u7801\u63d0\u6743\u4f1a\u91cd\u65b0\u8fd0\u884c\u81ea\u8eab\uff0c\u90a3\u4e48\u8c03\u8bd5\u542f\u52a8\u7684\u8fdb\u7a0b\u5c31\u7ed3\u675f\u4e86\uff0c\u4f1a\u8fde\u5e26\u628a\u8c03\u8bd5\u542f\u52a8\u7684\u8fdb\u7a0b\u5c1d\u8bd5\u63d0\u6743\u65b0\u8fd0\u884c\u7684\u62e5\u6709\u7ba1\u7406\u5458\u6743\u9650\u7684\u8fdb\u7a0b\u4e00\u8d77\u5173\u95ed\uff0c\u4e5f\u5c31\u662f\u51fa\u73b0\u95ea\u9000\u7684\u60c5\u51b5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6d4b\u8bd5\u73af\u5883 Windows 11 24H2 Python 3.13.1 \u5b9e\u73b0 import sys import [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"zakra_page_container_layout":"customizer","zakra_page_sidebar_layout":"customizer","zakra_remove_content_margin":false,"zakra_sidebar":"customizer","zakra_transparent_header":"customizer","zakra_logo":0,"zakra_main_header_style":"default","zakra_menu_item_color":"","zakra_menu_item_hover_color":"","zakra_menu_item_active_color":"","zakra_menu_active_style":"","zakra_page_header":true,"_lmt_disableupdate":"no","_lmt_disable":"no","footnotes":""},"categories":[1,592,611],"tags":[],"class_list":["post-18238","post","type-post","status-publish","format-standard","hentry","category-all","category-python","category-windows"],"modified_by":"IYATT-yx","_links":{"self":[{"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=\/wp\/v2\/posts\/18238","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=18238"}],"version-history":[{"count":12,"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=\/wp\/v2\/posts\/18238\/revisions"}],"predecessor-version":[{"id":18754,"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=\/wp\/v2\/posts\/18238\/revisions\/18754"}],"wp:attachment":[{"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=18238"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=18238"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=18238"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}