{"id":946492,"date":"2024-12-26T23:42:50","date_gmt":"2024-12-26T15:42:50","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/946492.html"},"modified":"2024-12-26T23:42:52","modified_gmt":"2024-12-26T15:42:52","slug":"python-%e5%a6%82%e4%bd%95%e5%8f%91%e5%b8%83exe","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/946492.html","title":{"rendered":"python \u5982\u4f55\u53d1\u5e03exe"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25082638\/04c77832-627f-469a-af70-904eba2e15bc.webp\" alt=\"python \u5982\u4f55\u53d1\u5e03exe\" \/><\/p>\n<p><p> <strong>\u8981\u5c06Python\u4ee3\u7801\u53d1\u5e03\u4e3a\u53ef\u6267\u884c\u6587\u4ef6\uff08.exe\uff09\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u5de5\u5177\u5982PyInstaller\u3001cx_Freeze\u3001py2exe\u7b49\u3002\u8fd9\u4e9b\u5de5\u5177\u53ef\u4ee5\u5e2e\u52a9\u60a8\u6253\u5305Python\u7a0b\u5e8f\u53ca\u5176\u6240\u6709\u4f9d\u8d56\u9879\uff0c\u4f7f\u5176\u53ef\u4ee5\u5728\u6ca1\u6709Python\u73af\u5883\u7684Windows\u7cfb\u7edf\u4e0a\u8fd0\u884c\u3002\u4ee5PyInstaller\u4e3a\u4f8b\uff0c\u5b83\u7b80\u5355\u3001\u7075\u6d3b\uff0c\u5e76\u652f\u6301\u591a\u79cd\u5e73\u53f0\u3002\u9996\u5148\uff0c\u60a8\u9700\u8981\u5b89\u88c5PyInstaller\uff0c\u7136\u540e\u901a\u8fc7\u547d\u4ee4\u884c\u5de5\u5177\u5c06Python\u811a\u672c\u6253\u5305\u4e3a\u53ef\u6267\u884c\u6587\u4ef6<\/strong>\u3002\u4e0b\u9762\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528PyInstaller\u521b\u5efaexe\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u5b89\u88c5PyInstaller<\/p>\n<\/p>\n<p><p>\u8981\u4f7f\u7528PyInstaller\uff0c\u9996\u5148\u9700\u8981\u786e\u4fdd\u5728\u60a8\u7684\u5f00\u53d1\u73af\u5883\u4e2d\u5b89\u88c5\u4e86\u5b83\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528pip\u5de5\u5177\u6765\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pyinstaller<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u547d\u4ee4<code>pyinstaller --version<\/code>\u6765\u9a8c\u8bc1\u5b89\u88c5\u662f\u5426\u6210\u529f\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u51c6\u5907Python\u811a\u672c<\/p>\n<\/p>\n<p><p>\u5728\u5f00\u59cb\u6253\u5305\u4e4b\u524d\uff0c\u786e\u4fdd\u60a8\u7684Python\u811a\u672c\u80fd\u591f\u5728\u672c\u5730\u73af\u5883\u4e0b\u6b63\u5e38\u8fd0\u884c\u3002\u53ef\u4ee5\u901a\u8fc7\u547d\u4ee4\u884c\u6216IDE\u8fdb\u884c\u6d4b\u8bd5\uff0c\u786e\u4fdd\u6ca1\u6709\u8bed\u6cd5\u9519\u8bef\u6216\u4f9d\u8d56\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528PyInstaller\u521b\u5efa\u53ef\u6267\u884c\u6587\u4ef6<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u57fa\u7840\u547d\u4ee4<\/strong><\/p>\n<\/p>\n<p><p>\u6253\u5f00\u547d\u4ee4\u63d0\u793a\u7b26\uff08Windows\uff09\u6216\u7ec8\u7aef\uff08macOS\/Linux\uff09\uff0c\u5bfc\u822a\u5230\u60a8\u7684Python\u811a\u672c\u6240\u5728\u7684\u76ee\u5f55\uff0c\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pyinstaller your_script.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u751f\u6210\u4e00\u4e2a\u5305\u542b\u591a\u4e2a\u6587\u4ef6\u548c\u6587\u4ef6\u5939\u7684dist\u76ee\u5f55\u3002\u5176\u4e2d\uff0c\u6700\u91cd\u8981\u7684\u662f<code>your_script.exe<\/code>\u6587\u4ef6\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4e00\u4e2a\u6587\u4ef6\u7684\u53ef\u6267\u884c\u6587\u4ef6<\/strong><\/p>\n<\/p>\n<p><p>\u5982\u679c\u60a8\u5e0c\u671b\u5c06\u6240\u6709\u5185\u5bb9\u6253\u5305\u6210\u4e00\u4e2a\u5355\u72ec\u7684\u53ef\u6267\u884c\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>--onefile<\/code>\u9009\u9879\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pyinstaller --onefile your_script.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6b64\u9009\u9879\u4f1a\u521b\u5efa\u4e00\u4e2a\u72ec\u7acb\u7684\u53ef\u6267\u884c\u6587\u4ef6\uff0c\u4fbf\u4e8e\u5206\u53d1\u548c\u7ba1\u7406\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6dfb\u52a0\u56fe\u6807<\/strong><\/p>\n<\/p>\n<p><p>\u60a8\u53ef\u4ee5\u4e3a\u60a8\u7684exe\u6587\u4ef6\u6dfb\u52a0\u4e00\u4e2a\u56fe\u6807\uff0c\u4f7f\u7528<code>--icon<\/code>\u9009\u9879\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pyinstaller --onefile --icon=your_icon.ico your_script.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8bf7\u786e\u4fdd\u56fe\u6807\u6587\u4ef6\uff08.ico\u683c\u5f0f\uff09\u4e0e\u60a8\u7684\u811a\u672c\u5728\u540c\u4e00\u4e2a\u76ee\u5f55\u4e2d\uff0c\u6216\u8005\u6307\u5b9a\u5b8c\u6574\u8def\u5f84\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u56db\u3001\u6253\u5305\u8fc7\u7a0b\u4e2d\u5e38\u89c1\u95ee\u9898\u53ca\u89e3\u51b3\u65b9\u6848<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u7f3a\u5931\u4f9d\u8d56<\/strong><\/p>\n<\/p>\n<p><p>PyInstaller\u5728\u6253\u5305\u8fc7\u7a0b\u4e2d\u4f1a\u81ea\u52a8\u68c0\u6d4b\u5e76\u6253\u5305\u5927\u90e8\u5206\u4f9d\u8d56\uff0c\u4f46\u6709\u65f6\u53ef\u80fd\u4f1a\u9057\u6f0f\u4e00\u4e9b\u6a21\u5757\u3002\u82e5\u51fa\u73b0\u8fd9\u6837\u7684\u60c5\u51b5\uff0c\u60a8\u53ef\u4ee5\u624b\u52a8\u6307\u5b9a\u9700\u8981\u7684\u6a21\u5757\uff0c\u4f7f\u7528<code>--hidden-import<\/code>\u53c2\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pyinstaller --onefile --hidden-import=module_name your_script.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8c03\u8bd5\u4fe1\u606f<\/strong><\/p>\n<\/p>\n<p><p>\u5982\u679c\u751f\u6210\u7684exe\u6587\u4ef6\u65e0\u6cd5\u8fd0\u884c\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u67e5\u770bPyInstaller\u751f\u6210\u7684\u65e5\u5fd7\u6587\u4ef6\u6765\u8c03\u8bd5\u9519\u8bef\u3002\u8fd9\u4e9b\u65e5\u5fd7\u6587\u4ef6\u901a\u5e38\u4fdd\u5b58\u5728<code>build<\/code>\u76ee\u5f55\u4e2d\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u517c\u5bb9\u6027\u95ee\u9898<\/strong><\/p>\n<\/p>\n<p><p>\u4e0d\u540c\u7248\u672c\u7684Python\u548cPyInstaller\u53ef\u80fd\u4f1a\u5bfc\u81f4\u517c\u5bb9\u6027\u95ee\u9898\u3002\u786e\u4fdd\u60a8\u4f7f\u7528\u7684PyInstaller\u7248\u672c\u4e0ePython\u7248\u672c\u517c\u5bb9\uff0c\u5e76\u67e5\u770bPyInstaller\u7684\u5b98\u65b9\u6587\u6863\u4ee5\u83b7\u53d6\u66f4\u591a\u652f\u6301\u4fe1\u606f\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e94\u3001\u53d1\u5e03\u4e0e\u5206\u53d1<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u6d4b\u8bd5<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u5c06\u751f\u6210\u7684exe\u6587\u4ef6\u5206\u53d1\u7ed9\u5176\u4ed6\u7528\u6237\u4e4b\u524d\uff0c\u52a1\u5fc5\u5728\u5e72\u51c0\u7684Windows\u73af\u5883\u4e2d\u8fdb\u884c\u6d4b\u8bd5\u3002\u53ef\u4ee5\u4f7f\u7528\u865a\u62df\u673a\u6216Docker\u955c\u50cf\u6765\u6a21\u62df\u6d4b\u8bd5\u73af\u5883\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5206\u53d1<\/strong><\/p>\n<\/p>\n<p><p>\u60a8\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5206\u53d1\u751f\u6210\u7684exe\u6587\u4ef6\uff0c\u4f8b\u5982\u901a\u8fc7\u7535\u5b50\u90ae\u4ef6\u3001\u4e91\u5b58\u50a8\u6216\u6587\u4ef6\u5171\u4eab\u670d\u52a1\u3002\u786e\u4fdd\u7528\u6237\u77e5\u9053\u5982\u4f55\u8fd0\u884cexe\u6587\u4ef6\u4ee5\u53ca\u9700\u8981\u7684\u4efb\u4f55\u5148\u51b3\u6761\u4ef6\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u66f4\u65b0<\/strong><\/p>\n<\/p>\n<p><p>\u5982\u679c\u60a8\u5bf9\u539f\u59cbPython\u811a\u672c\u8fdb\u884c\u4e86\u66f4\u65b0\uff0c\u9700\u8981\u91cd\u65b0\u8fd0\u884cPyInstaller\u547d\u4ee4\u4ee5\u751f\u6210\u65b0\u7684exe\u6587\u4ef6\uff0c\u5e76\u901a\u77e5\u7528\u6237\u4e0b\u8f7d\u6700\u65b0\u7248\u672c\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u516d\u3001\u5176\u4ed6\u5de5\u5177\u7684\u4f7f\u7528<\/p>\n<\/p>\n<p><p>\u5c3d\u7ba1PyInstaller\u662f\u6700\u5e38\u7528\u7684\u5de5\u5177\u4e4b\u4e00\uff0c\u5176\u4ed6\u5de5\u5177\u5982cx_Freeze\u548cpy2exe\u4e5f\u53ef\u4ee5\u7528\u4e8e\u5c06Python\u811a\u672c\u6253\u5305\u6210exe\u6587\u4ef6\u3002\u6bcf\u79cd\u5de5\u5177\u90fd\u6709\u5176\u72ec\u7279\u7684\u529f\u80fd\u548c\u8bbe\u7f6e\uff0c\u7528\u6237\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u5de5\u5177\u3002<\/p>\n<\/p>\n<p><p>\u603b\u4e4b\uff0c\u5c06Python\u4ee3\u7801\u6253\u5305\u6210exe\u6587\u4ef6\u662f\u4e00\u4e2a\u76f8\u5bf9\u7b80\u5355\u7684\u8fc7\u7a0b\uff0c\u4f46\u4e5f\u9700\u8981\u6ce8\u610f\u4f9d\u8d56\u7ba1\u7406\u548c\u517c\u5bb9\u6027\u95ee\u9898\u3002\u901a\u8fc7PyInstaller\u7b49\u5de5\u5177\uff0c\u60a8\u53ef\u4ee5\u8f7b\u677e\u5730\u5c06Python\u7a0b\u5e8f\u53d8\u4e3a\u53ef\u5728Windows\u7cfb\u7edf\u4e0a\u72ec\u7acb\u8fd0\u884c\u7684\u5e94\u7528\u7a0b\u5e8f\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5c06Python\u7a0b\u5e8f\u8f6c\u6362\u4e3a\u53ef\u6267\u884c\u6587\u4ef6\uff1f<\/strong><br \/>\u5c06Python\u7a0b\u5e8f\u8f6c\u6362\u4e3a\u53ef\u6267\u884c\u6587\u4ef6\u7684\u5e38\u7528\u65b9\u6cd5\u662f\u4f7f\u7528PyInstaller\u6216cx_Freeze\u7b49\u5de5\u5177\u3002\u8fd9\u4e9b\u5de5\u5177\u53ef\u4ee5\u5c06Python\u811a\u672c\u6253\u5305\u6210\u72ec\u7acb\u7684exe\u6587\u4ef6\u3002\u5728\u4f7f\u7528PyInstaller\u65f6\uff0c\u53ea\u9700\u5728\u547d\u4ee4\u884c\u4e2d\u8fd0\u884c<code>pyinstaller your_script.py<\/code>\uff0c\u4fbf\u53ef\u4ee5\u751f\u6210exe\u6587\u4ef6\u3002\u786e\u4fdd\u5728\u8fd0\u884c\u547d\u4ee4\u4e4b\u524d\u5df2\u5b89\u88c5\u597d\u76f8\u5173\u7684\u5e93\u3002<\/p>\n<p><strong>\u751f\u6210\u7684exe\u6587\u4ef6\u5728\u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\u4e0a\u662f\u5426\u517c\u5bb9\uff1f<\/strong><br \/>\u751f\u6210\u7684exe\u6587\u4ef6\u901a\u5e38\u53ea\u80fd\u5728\u4e0e\u5176\u521b\u5efa\u65f6\u76f8\u540c\u7684\u64cd\u4f5c\u7cfb\u7edf\u4e0a\u8fd0\u884c\u3002\u4f8b\u5982\uff0c\u4f7f\u7528Windows\u521b\u5efa\u7684exe\u6587\u4ef6\u65e0\u6cd5\u5728Mac\u6216Linux\u4e0a\u8fd0\u884c\u3002\u5982\u679c\u5e0c\u671b\u5728\u4e0d\u540c\u7cfb\u7edf\u4e0a\u4f7f\u7528\u7a0b\u5e8f\uff0c\u5efa\u8bae\u4e3a\u6bcf\u4e2a\u64cd\u4f5c\u7cfb\u7edf\u5206\u522b\u751f\u6210\u5bf9\u5e94\u7684\u53ef\u6267\u884c\u6587\u4ef6\u3002<\/p>\n<p><strong>\u53d1\u5e03exe\u6587\u4ef6\u65f6\u5e94\u8be5\u6ce8\u610f\u54ea\u4e9b\u95ee\u9898\uff1f<\/strong><br \/>\u53d1\u5e03exe\u6587\u4ef6\u524d\u9700\u8981\u6ce8\u610f\u51e0\u4e2a\u95ee\u9898\uff0c\u5305\u62ec\u786e\u4fdd\u6240\u6709\u4f9d\u8d56\u5e93\u5df2\u7ecf\u6253\u5305\u3001\u5408\u7406\u8bbe\u7f6e\u7a0b\u5e8f\u7684\u56fe\u6807\u548c\u7248\u672c\u4fe1\u606f\uff0c\u4ee5\u53ca\u7f16\u5199\u6e05\u6670\u7684\u7528\u6237\u6587\u6863\u3002\u6b64\u5916\uff0c\u6d4b\u8bd5exe\u6587\u4ef6\u5728\u76ee\u6807\u673a\u5668\u4e0a\u7684\u517c\u5bb9\u6027\u548c\u8fd0\u884c\u6548\u679c\u4e5f\u975e\u5e38\u91cd\u8981\uff0c\u4ee5\u786e\u4fdd\u7528\u6237\u80fd\u591f\u987a\u5229\u4f7f\u7528\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u5c06Python\u4ee3\u7801\u53d1\u5e03\u4e3a\u53ef\u6267\u884c\u6587\u4ef6\uff08.exe\uff09\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u5de5\u5177\u5982PyInstaller\u3001cx_Freeze\u3001 [&hellip;]","protected":false},"author":3,"featured_media":946500,"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\/946492"}],"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=946492"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/946492\/revisions"}],"predecessor-version":[{"id":946501,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/946492\/revisions\/946501"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/946500"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=946492"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=946492"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=946492"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}