{"id":947312,"date":"2024-12-26T23:50:25","date_gmt":"2024-12-26T15:50:25","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/947312.html"},"modified":"2024-12-26T23:50:28","modified_gmt":"2024-12-26T15:50:28","slug":"python%e5%a6%82%e4%bd%95%e5%8f%98%e6%88%90exe","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/947312.html","title":{"rendered":"python\u5982\u4f55\u53d8\u6210exe"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25083134\/01b44540-9df3-4686-9ed3-111558b4e0f2.webp\" alt=\"python\u5982\u4f55\u53d8\u6210exe\" \/><\/p>\n<p><p> \u5c06Python\u811a\u672c\u8f6c\u6362\u4e3a\u53ef\u6267\u884c\u6587\u4ef6\uff08.exe\uff09\u662f\u4e00\u9879\u5e38\u89c1\u7684\u4efb\u52a1\uff0c\u5c24\u5176\u662f\u5728\u9700\u8981\u5206\u4eab\u5e94\u7528\u7a0b\u5e8f\u7ed9\u4e0d\u5177\u5907Python\u73af\u5883\u7684\u7528\u6237\u65f6\u3002<strong>\u8981\u5c06Python\u4ee3\u7801\u53d8\u6210\u4e00\u4e2a\u53ef\u6267\u884c\u6587\u4ef6\uff08.exe\uff09\uff0c\u53ef\u4ee5\u4f7f\u7528\u5de5\u5177\u5982PyInstaller\u3001cx_Freeze\u3001py2exe<\/strong>\u3002\u5176\u4e2d\uff0cPyInstaller\u662f\u6700\u5e38\u7528\u4e14\u6613\u7528\u7684\u5de5\u5177\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528PyInstaller\u5c06Python\u811a\u672c\u8f6c\u6362\u4e3a.exe\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h3>PYINSTALLER\u7684\u5b89\u88c5\u4e0e\u57fa\u672c\u4f7f\u7528<\/h3>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b89\u88c5PyInstaller<\/strong>\uff1a\u9996\u5148\uff0c\u9700\u8981\u786e\u4fdd\u4f60\u7684Python\u73af\u5883\u5df2\u7ecf\u914d\u7f6e\u597d\uff0c\u7136\u540e\u901a\u8fc7pip\u547d\u4ee4\u5b89\u88c5PyInstaller\u3002\u6253\u5f00\u547d\u4ee4\u63d0\u793a\u7b26\u6216\u7ec8\u7aef\uff0c\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pyinstaller<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8f6c\u6362Python\u811a\u672c\u4e3a.exe<\/strong>\uff1a\u5047\u8bbe\u4f60\u6709\u4e00\u4e2aPython\u811a\u672c\u6587\u4ef6<code>my_script.py<\/code>\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u5c06\u5176\u8f6c\u6362\u4e3a\u53ef\u6267\u884c\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pyinstaller --onefile my_script.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u91cc\u7684<code>--onefile<\/code>\u53c2\u6570\u8868\u793a\u5c06\u6240\u6709\u7684\u6587\u4ef6\u6253\u5305\u5230\u4e00\u4e2a\u53ef\u6267\u884c\u6587\u4ef6\u4e2d\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u751f\u6210\u7684\u6587\u4ef6\u4f4d\u7f6e<\/strong>\uff1a\u5b8c\u6210\u540e\uff0cPyInstaller\u4f1a\u521b\u5efa\u4e00\u4e2a<code>dist<\/code>\u76ee\u5f55\uff0c\u751f\u6210\u7684.exe\u6587\u4ef6\u5c31\u5728\u8fd9\u4e2a\u76ee\u5f55\u4e0b\u3002\u4f60\u53ef\u4ee5\u5c06\u8fd9\u4e2a\u6587\u4ef6\u5206\u53d1\u7ed9\u5176\u4ed6\u7528\u6237\u8fd0\u884c\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>PYINSTALLER\u7684\u9ad8\u7ea7\u9009\u9879<\/h3>\n<\/p>\n<ol>\n<li>\n<p><strong>\u6dfb\u52a0\u56fe\u6807<\/strong>\uff1a\u5982\u679c\u4f60\u5e0c\u671b\u4f60\u7684\u5e94\u7528\u7a0b\u5e8f\u6709\u4e00\u4e2a\u81ea\u5b9a\u4e49\u7684\u56fe\u6807\uff0c\u53ef\u4ee5\u4f7f\u7528<code>--icon<\/code>\u9009\u9879\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pyinstaller --onefile --icon=my_icon.ico my_script.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u9690\u85cf\u63a7\u5236\u53f0<\/strong>\uff1a\u5bf9\u4e8eGUI\u5e94\u7528\u7a0b\u5e8f\uff0c\u4f60\u53ef\u80fd\u4e0d\u5e0c\u671b\u770b\u5230\u4e00\u4e2a\u63a7\u5236\u53f0\u7a97\u53e3\uff0c\u8fd9\u65f6\u53ef\u4ee5\u4f7f\u7528<code>--noconsole<\/code>\u9009\u9879\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pyinstaller --onefile --noconsole my_script.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u9644\u52a0\u6570\u636e\u6587\u4ef6<\/strong>\uff1a\u5982\u679c\u4f60\u7684\u7a0b\u5e8f\u9700\u8981\u9644\u52a0\u7684\u6587\u4ef6\uff08\u5982\u914d\u7f6e\u6587\u4ef6\u3001\u6570\u636e\u6587\u4ef6\u7b49\uff09\uff0c\u53ef\u4ee5\u4f7f\u7528<code>--add-data<\/code>\u9009\u9879\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pyinstaller --onefile --add-data &#39;data.txt;.&#39; my_script.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u91cc\u7684<code>&#39;data.txt;.&#39;<\/code>\u8868\u793a\u5c06<code>data.txt<\/code>\u6587\u4ef6\u6dfb\u52a0\u5230\u540c\u4e00\u76ee\u5f55\u4e2d\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u5e38\u89c1\u95ee\u9898\u53ca\u89e3\u51b3\u65b9\u6848<\/h3>\n<\/p>\n<ol>\n<li>\n<p><strong>\u6253\u5305\u65f6\u95f4\u957f\u6216\u751f\u6210\u6587\u4ef6\u8fc7\u5927<\/strong>\uff1a\u8fd9\u662f\u56e0\u4e3aPyInstaller\u4f1a\u5c06\u6240\u6709\u4f9d\u8d56\u6253\u5305\u5230\u4e00\u8d77\u3002\u53ef\u4ee5\u901a\u8fc7<code>--onedir<\/code>\u9009\u9879\u521b\u5efa\u4e00\u4e2a\u76ee\u5f55\u800c\u4e0d\u662f\u5355\u4e2a\u6587\u4ef6\uff0c\u4ee5\u51cf\u5c11\u5355\u4e2a\u6587\u4ef6\u7684\u5927\u5c0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pyinstaller --onedir my_script.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6253\u5305\u540e\u7684\u6587\u4ef6\u8fd0\u884c\u65f6\u51fa\u9519<\/strong>\uff1a\u8fd9\u53ef\u80fd\u662f\u56e0\u4e3a\u67d0\u4e9b\u6a21\u5757\u672a\u6b63\u786e\u6253\u5305\u3002\u53ef\u4ee5\u5c1d\u8bd5\u624b\u52a8\u6dfb\u52a0\u8fd9\u4e9b\u6a21\u5757\uff0c\u6216\u8005\u67e5\u770bPyInstaller\u751f\u6210\u7684.spec\u6587\u4ef6\u8fdb\u884c\u8c03\u6574\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u517c\u5bb9\u6027\u95ee\u9898<\/strong>\uff1a\u6253\u5305\u7684.exe\u6587\u4ef6\u901a\u5e38\u53ea\u80fd\u5728\u76f8\u540c\u67b6\u6784\u7684\u7cfb\u7edf\u4e0a\u8fd0\u884c\u3002\u5982\u679c\u9700\u8981\u5728\u4e0d\u540c\u5e73\u53f0\uff08\u5982Linux\u3001macOS\uff09\u4e0a\u8fd0\u884c\uff0c\u9700\u8981\u5728\u5bf9\u5e94\u5e73\u53f0\u4e0a\u8fdb\u884c\u6253\u5305\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u4f7f\u7528CX_FREEZE<\/h3>\n<\/p>\n<p><p>\u9664\u4e86PyInstaller\uff0ccx_Freeze\u4e5f\u662f\u4e00\u79cd\u5e38\u7528\u7684\u5de5\u5177\uff0c\u9002\u7528\u4e8e\u591a\u5e73\u53f0\u7684Python\u5e94\u7528\u7a0b\u5e8f\u6253\u5305\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b89\u88c5cx_Freeze<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install cx_Freeze<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u521b\u5efasetup\u811a\u672c<\/strong>\uff1a\u521b\u5efa\u4e00\u4e2a<code>setup.py<\/code>\u6587\u4ef6\u7528\u4e8e\u914d\u7f6e\u6253\u5305\u9009\u9879\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from cx_Freeze import setup, Executable<\/p>\n<p>setup(<\/p>\n<p>    name = &quot;my_program&quot;,<\/p>\n<p>    version = &quot;0.1&quot;,<\/p>\n<p>    description = &quot;My Python Program&quot;,<\/p>\n<p>    executables = [Executable(&quot;my_script.py&quot;)]<\/p>\n<p>)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8fd0\u884c\u6253\u5305\u547d\u4ee4<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">python setup.py build<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u5728<code>build<\/code>\u76ee\u5f55\u4e2d\u751f\u6210\u53ef\u6267\u884c\u6587\u4ef6\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u4f7f\u7528PY2EXE<\/h3>\n<\/p>\n<p><p>py2exe\u662f\u4e13\u95e8\u4e3aWindows\u5e73\u53f0\u8bbe\u8ba1\u7684Python\u6253\u5305\u5de5\u5177\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b89\u88c5py2exe<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install py2exe<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u521b\u5efasetup\u811a\u672c<\/strong>\uff1a\u4e0ecx_Freeze\u7c7b\u4f3c\uff0c\u9700\u8981\u4e00\u4e2a<code>setup.py<\/code>\u811a\u672c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from distutils.core import setup<\/p>\n<p>import py2exe<\/p>\n<p>setup(console=[&#39;my_script.py&#39;])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8fd0\u884c\u6253\u5305\u547d\u4ee4<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">python setup.py py2exe<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u5c0f\u7ed3<\/h3>\n<\/p>\n<p><p>\u5c06Python\u811a\u672c\u8f6c\u6362\u4e3a.exe\u6587\u4ef6\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c<strong>PyInstaller\u662f\u6700\u63a8\u8350\u7684\u65b9\u6cd5<\/strong>\uff0c\u56e0\u4e3a\u5b83\u652f\u6301\u5927\u591a\u6570Python\u5e93\uff0c\u4e14\u6613\u4e8e\u4f7f\u7528\u3002\u65e0\u8bba\u9009\u62e9\u54ea\u79cd\u5de5\u5177\uff0c\u90fd\u9700\u6839\u636e\u9879\u76ee\u7684\u5177\u4f53\u9700\u6c42\u8fdb\u884c\u914d\u7f6e\u548c\u4f18\u5316\u3002\u638c\u63e1\u8fd9\u4e9b\u5de5\u5177\u7684\u4f7f\u7528\uff0c\u53ef\u4ee5\u5927\u5927\u63d0\u9ad8Python\u5e94\u7528\u7684\u53ef\u79fb\u690d\u6027\u548c\u7528\u6237\u4f53\u9a8c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5c06Python\u811a\u672c\u8f6c\u6362\u4e3a\u53ef\u6267\u884c\u6587\u4ef6\uff1f<\/strong><br \/>\u8981\u5c06Python\u811a\u672c\u8f6c\u6362\u4e3a.exe\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e9b\u6d41\u884c\u7684\u5de5\u5177\uff0c\u4f8b\u5982PyInstaller\u3001cx_Freeze\u6216py2exe\u3002\u8fd9\u4e9b\u5de5\u5177\u53ef\u4ee5\u5c06Python\u4ee3\u7801\u6253\u5305\u5e76\u751f\u6210\u4e00\u4e2a\u72ec\u7acb\u7684\u53ef\u6267\u884c\u6587\u4ef6\uff0c\u7528\u6237\u65e0\u9700\u5728\u5176\u7cfb\u7edf\u4e0a\u5b89\u88c5Python\u73af\u5883\u5373\u53ef\u8fd0\u884c\u3002\u4ee5PyInstaller\u4e3a\u4f8b\uff0c\u60a8\u53ea\u9700\u5728\u547d\u4ee4\u884c\u4e2d\u8fd0\u884c<code>pyinstaller your_script.py<\/code>\uff0c\u5373\u53ef\u751f\u6210\u53ef\u6267\u884c\u6587\u4ef6\u3002<\/p>\n<p><strong>\u8f6c\u6362\u540e\u751f\u6210\u7684.exe\u6587\u4ef6\u53ef\u4ee5\u5728\u5176\u4ed6\u64cd\u4f5c\u7cfb\u7edf\u4e0a\u8fd0\u884c\u5417\uff1f<\/strong><br \/>\u751f\u6210\u7684.exe\u6587\u4ef6\u4e3b\u8981\u662f\u5728Windows\u64cd\u4f5c\u7cfb\u7edf\u4e0a\u8fd0\u884c\u7684\u3002\u5982\u679c\u60a8\u5e0c\u671b\u5728\u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\u4e0a\u4f7f\u7528\u76f8\u540c\u7684\u4ee3\u7801\uff0c\u5efa\u8bae\u5728\u76f8\u5e94\u7684\u64cd\u4f5c\u7cfb\u7edf\u4e0a\u4f7f\u7528\u76f8\u5e94\u7684\u5de5\u5177\u8fdb\u884c\u6253\u5305\u3002\u4f8b\u5982\uff0c\u4f7f\u7528PyInstaller\u5728Linux\u4e0a\u751f\u6210\u7684\u53ef\u6267\u884c\u6587\u4ef6\u53ea\u80fd\u5728Linux\u73af\u5883\u4e2d\u8fd0\u884c\u3002<\/p>\n<p><strong>\u8f6c\u6362\u8fc7\u7a0b\u4e2d\u662f\u5426\u4f1a\u5f71\u54cdPython\u7a0b\u5e8f\u7684\u6027\u80fd\uff1f<\/strong><br \/>\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u751f\u6210\u7684.exe\u6587\u4ef6\u5728\u6027\u80fd\u4e0a\u4e0e\u539f\u59cbPython\u811a\u672c\u76f8\u4f3c\u3002\u4f46\u662f\uff0c\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u6253\u5305\u53ef\u80fd\u4f1a\u5f15\u5165\u989d\u5916\u7684\u4f9d\u8d56\u9879\uff0c\u4ece\u800c\u5f71\u54cd\u542f\u52a8\u65f6\u95f4\u3002\u5982\u679c\u7a0b\u5e8f\u4e2d\u5305\u542b\u5927\u91cf\u5916\u90e8\u5e93\uff0c\u6253\u5305\u540e\u7684\u6587\u4ef6\u53ef\u80fd\u4f1a\u53d8\u5f97\u8f83\u5927\uff0c\u5e76\u4e14\u52a0\u8f7d\u65f6\u95f4\u53ef\u80fd\u7a0d\u5fae\u5ef6\u957f\u3002\u56e0\u6b64\uff0c\u5efa\u8bae\u5728\u6253\u5305\u4e4b\u524d\u8fdb\u884c\u6027\u80fd\u6d4b\u8bd5\uff0c\u4ee5\u786e\u4fdd\u6700\u7ec8\u4ea7\u54c1\u7684\u8868\u73b0\u7b26\u5408\u9884\u671f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5c06Python\u811a\u672c\u8f6c\u6362\u4e3a\u53ef\u6267\u884c\u6587\u4ef6\uff08.exe\uff09\u662f\u4e00\u9879\u5e38\u89c1\u7684\u4efb\u52a1\uff0c\u5c24\u5176\u662f\u5728\u9700\u8981\u5206\u4eab\u5e94\u7528\u7a0b\u5e8f\u7ed9\u4e0d\u5177\u5907Python\u73af [&hellip;]","protected":false},"author":3,"featured_media":947318,"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\/947312"}],"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=947312"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/947312\/revisions"}],"predecessor-version":[{"id":947319,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/947312\/revisions\/947319"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/947318"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=947312"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=947312"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=947312"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}