{"id":1086960,"date":"2025-01-08T13:30:43","date_gmt":"2025-01-08T05:30:43","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1086960.html"},"modified":"2025-01-08T13:30:46","modified_gmt":"2025-01-08T05:30:46","slug":"%e5%a6%82%e4%bd%95python%e6%96%87%e4%bb%b6%e7%bc%96%e8%af%91%e6%88%90exe%e6%96%87%e4%bb%b6-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1086960.html","title":{"rendered":"\u5982\u4f55python\u6587\u4ef6\u7f16\u8bd1\u6210exe\u6587\u4ef6"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24200128\/8b8c9f0c-dbef-401e-84c3-e3f5ce93976b.webp\" alt=\"\u5982\u4f55python\u6587\u4ef6\u7f16\u8bd1\u6210exe\u6587\u4ef6\" \/><\/p>\n<p><p> <strong>\u8981\u5c06Python\u6587\u4ef6\u7f16\u8bd1\u6210exe\u6587\u4ef6\uff0c\u4e3b\u8981\u6709\u4ee5\u4e0b\u51e0\u79cd\u65b9\u6cd5\uff1a\u4f7f\u7528PyInstaller\u3001\u4f7f\u7528cx_Freeze\u3001\u4f7f\u7528Py2exe\u3001\u4f7f\u7528Nuitka\u3002<\/strong> \u5176\u4e2d\uff0c\u6700\u5e38\u7528\u548c\u6700\u63a8\u8350\u7684\u65b9\u6cd5\u662f\u4f7f\u7528PyInstaller\uff0c\u56e0\u4e3a\u5b83\u64cd\u4f5c\u7b80\u5355\u3001\u529f\u80fd\u5f3a\u5927\u3001\u517c\u5bb9\u6027\u597d\u3002<strong>\u4f7f\u7528PyInstaller\u7684\u6b65\u9aa4\u5305\u62ec\u5b89\u88c5PyInstaller\u3001\u7f16\u5199Python\u4ee3\u7801\u3001\u4f7f\u7528PyInstaller\u6253\u5305\u6210exe\u6587\u4ef6\u3001\u4f18\u5316\u548c\u914d\u7f6e\u6253\u5305\u8fc7\u7a0b\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u8be6\u7ec6\u7684\u6b65\u9aa4\u548c\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5b89\u88c5PyInstaller<\/h3>\n<\/p>\n<p><p>PyInstaller\u662f\u4e00\u4e2a\u53ef\u4ee5\u5c06Python\u5e94\u7528\u7a0b\u5e8f\u6253\u5305\u6210\u72ec\u7acb\u7684\u53ef\u6267\u884c\u6587\u4ef6\u7684\u5de5\u5177\u3002\u9996\u5148\uff0c\u4f60\u9700\u8981\u5b89\u88c5PyInstaller\u3002\u53ef\u4ee5\u901a\u8fc7pip\u547d\u4ee4\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><h3>\u4e8c\u3001\u7f16\u5199Python\u4ee3\u7801<\/h3>\n<\/p>\n<p><p>\u7f16\u5199\u4f60\u9700\u8981\u8f6c\u6362\u6210exe\u6587\u4ef6\u7684Python\u4ee3\u7801\uff0c\u5e76\u4fdd\u5b58\u4e3a\u4e00\u4e2a.py\u6587\u4ef6\u3002\u4f8b\u5982\uff0c\u6211\u4eec\u7f16\u5199\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\u4ee3\u7801\uff0c\u4fdd\u5b58\u4e3a<code>example.py<\/code>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">print(&quot;Hello, world!&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528PyInstaller\u6253\u5305\u6210exe\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u6253\u5f00\u547d\u4ee4\u884c\uff0c\u5bfc\u822a\u5230Python\u6587\u4ef6\u6240\u5728\u7684\u76ee\u5f55\uff0c\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pyinstaller --onefile example.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u9762\u7684\u547d\u4ee4\u4f1a\u751f\u6210\u4e00\u4e2adist\u76ee\u5f55\uff0c\u91cc\u9762\u5305\u542b\u4e00\u4e2a\u72ec\u7acb\u7684exe\u6587\u4ef6\u3002<code>--onefile<\/code>\u53c2\u6570\u8868\u793a\u5c06\u6240\u6709\u4f9d\u8d56\u6253\u5305\u6210\u4e00\u4e2a\u5355\u72ec\u7684exe\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f18\u5316\u548c\u914d\u7f6e\u6253\u5305\u8fc7\u7a0b<\/h3>\n<\/p>\n<ol>\n<li>\n<p><strong>\u6dfb\u52a0\u56fe\u6807<\/strong>\uff1a\u4f60\u53ef\u4ee5\u4e3aexe\u6587\u4ef6\u6dfb\u52a0\u4e00\u4e2a\u56fe\u6807\u3002\u9996\u5148\u51c6\u5907\u4e00\u4e2a.ico\u683c\u5f0f\u7684\u56fe\u6807\u6587\u4ef6\uff0c\u7136\u540e\u5728\u547d\u4ee4\u4e2d\u6dfb\u52a0<code>--icon<\/code>\u53c2\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pyinstaller --onefile --icon=icon.ico example.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u9690\u85cf\u63a7\u5236\u53f0\u7a97\u53e3<\/strong>\uff1a\u5982\u679c\u4f60\u7684\u5e94\u7528\u7a0b\u5e8f\u662f\u4e00\u4e2aGUI\u5e94\u7528\u7a0b\u5e8f\uff0c\u800c\u4e0d\u662f\u63a7\u5236\u53f0\u5e94\u7528\u7a0b\u5e8f\uff0c\u53ef\u4ee5\u4f7f\u7528<code>--windowed<\/code>\u53c2\u6570\u9690\u85cf\u63a7\u5236\u53f0\u7a97\u53e3\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pyinstaller --onefile --windowed example.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u914d\u7f6e\u6253\u5305\u8fc7\u7a0b<\/strong>\uff1aPyInstaller\u63d0\u4f9b\u4e86\u4e00\u4e2a.spec\u6587\u4ef6\u7528\u4e8e\u914d\u7f6e\u6253\u5305\u8fc7\u7a0b\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u7f16\u8f91.spec\u6587\u4ef6\u8fdb\u884c\u66f4\u9ad8\u7ea7\u7684\u914d\u7f6e\u3002\u4f8b\u5982\uff0c\u751f\u6210.spec\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pyinstaller --onefile example.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p> \u8fd9\u4f1a\u751f\u6210\u4e00\u4e2a<code>example.spec<\/code>\u6587\u4ef6\uff0c\u4f60\u53ef\u4ee5\u7f16\u8f91\u8fd9\u4e2a\u6587\u4ef6\u6765\u914d\u7f6e\u6253\u5305\u8fc7\u7a0b\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u4e94\u3001\u5176\u4ed6\u65b9\u6cd5<\/h3>\n<\/p>\n<p><h4>1\u3001\u4f7f\u7528cx_Freeze<\/h4>\n<\/p>\n<p><p>cx_Freeze\u662f\u53e6\u4e00\u4e2a\u5e38\u7528\u7684Python\u6253\u5305\u5de5\u5177\u3002\u5b83\u7684\u4f7f\u7528\u65b9\u6cd5\u4e5f\u975e\u5e38\u7b80\u5355\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p>\u5b89\u88c5cx_Freeze\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>\u521b\u5efa\u4e00\u4e2asetup\u811a\u672c\uff0c\u5982<code>setup.py<\/code>\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;example&quot;,<\/p>\n<p>    version=&quot;0.1&quot;,<\/p>\n<p>    description=&quot;My Python application&quot;,<\/p>\n<p>    executables=[Executable(&quot;example.py&quot;)]<\/p>\n<p>)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p>\u8fd0\u884csetup\u811a\u672c\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\u4f1a\u5728build\u76ee\u5f55\u4e0b\u751f\u6210\u53ef\u6267\u884c\u6587\u4ef6\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><h4>2\u3001\u4f7f\u7528Py2exe<\/h4>\n<\/p>\n<p><p>Py2exe\u662f\u4e00\u4e2a\u5c06Python\u811a\u672c\u6253\u5305\u6210Windows\u53ef\u6267\u884c\u7a0b\u5e8f\u7684\u5de5\u5177\uff0c\u4f46\u5b83\u4ec5\u652f\u6301Windows\u7cfb\u7edf\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p>\u5b89\u88c5Py2exe\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>\u521b\u5efa\u4e00\u4e2asetup\u811a\u672c\uff0c\u5982<code>setup.py<\/code>\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;example.py&#39;])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p>\u8fd0\u884csetup\u811a\u672c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">python setup.py py2exe<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p> \u8fd9\u4f1a\u5728dist\u76ee\u5f55\u4e0b\u751f\u6210\u53ef\u6267\u884c\u6587\u4ef6\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><h4>3\u3001\u4f7f\u7528Nuitka<\/h4>\n<\/p>\n<p><p>Nuitka\u662f\u4e00\u6b3e\u5c06Python\u4ee3\u7801\u7f16\u8bd1\u4e3aC++\u4ee3\u7801\u5e76\u751f\u6210\u53ef\u6267\u884c\u6587\u4ef6\u7684\u5de5\u5177\uff0c\u5177\u6709\u8f83\u9ad8\u7684\u6027\u80fd\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p>\u5b89\u88c5Nuitka\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install nuitka<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p>\u8fd0\u884cNuitka\u8fdb\u884c\u7f16\u8bd1\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">nuitka --standalone --mingw64 example.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p> \u8fd9\u4f1a\u751f\u6210\u4e00\u4e2adist\u76ee\u5f55\uff0c\u91cc\u9762\u5305\u542b\u53ef\u6267\u884c\u6587\u4ef6\u548c\u6240\u9700\u7684\u6240\u6709\u4f9d\u8d56\u9879\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u516d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p><strong>\u5c06Python\u6587\u4ef6\u7f16\u8bd1\u6210exe\u6587\u4ef6\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff0c\u5176\u4e2dPyInstaller\u662f\u6700\u5e38\u7528\u548c\u6700\u63a8\u8350\u7684\u65b9\u6cd5\u3002<\/strong> \u5b83\u64cd\u4f5c\u7b80\u5355\u3001\u529f\u80fd\u5f3a\u5927\uff0c\u5e76\u4e14\u652f\u6301\u591a\u79cd\u914d\u7f6e\u9009\u9879\uff0c\u53ef\u4ee5\u6ee1\u8db3\u5927\u591a\u6570\u9700\u6c42\u3002<strong>\u4f7f\u7528PyInstaller\u7684\u6b65\u9aa4\u5305\u62ec\u5b89\u88c5PyInstaller\u3001\u7f16\u5199Python\u4ee3\u7801\u3001\u4f7f\u7528PyInstaller\u6253\u5305\u6210exe\u6587\u4ef6\u3001\u4f18\u5316\u548c\u914d\u7f6e\u6253\u5305\u8fc7\u7a0b\u3002<\/strong> \u5176\u4ed6\u65b9\u6cd5\u5982cx_Freeze\u3001Py2exe\u548cNuitka\u4e5f\u5404\u6709\u4f18\u70b9\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u5de5\u5177\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5c06Python\u6587\u4ef6\u8f6c\u6362\u4e3a\u53ef\u6267\u884c\u6587\u4ef6\uff1f<\/strong><br \/>\u5c06Python\u6587\u4ef6\u8f6c\u6362\u4e3a\u53ef\u6267\u884c\u6587\u4ef6\u7684\u8fc7\u7a0b\u901a\u5e38\u6d89\u53ca\u4f7f\u7528\u4e00\u4e9b\u5de5\u5177\uff0c\u4f8b\u5982PyInstaller\u6216cx_Freeze\u3002\u8fd9\u4e9b\u5de5\u5177\u53ef\u4ee5\u5c06\u4f60\u7684Python\u811a\u672c\u6253\u5305\u6210\u4e00\u4e2a\u72ec\u7acb\u7684\u53ef\u6267\u884c\u6587\u4ef6\uff0c\u7528\u6237\u65e0\u9700\u5b89\u88c5Python\u73af\u5883\u5373\u53ef\u8fd0\u884c\u3002\u5177\u4f53\u6b65\u9aa4\u5305\u62ec\uff1a\u5b89\u88c5\u76f8\u5e94\u7684\u5de5\u5177\u3001\u5728\u547d\u4ee4\u884c\u4e2d\u8fd0\u884c\u6253\u5305\u547d\u4ee4\uff0c\u5e76\u6839\u636e\u9700\u8981\u914d\u7f6e\u4e00\u4e9b\u9009\u9879\u3002\u6700\u7ec8\u751f\u6210\u7684exe\u6587\u4ef6\u53ef\u4ee5\u5728Windows\u7cfb\u7edf\u4e0a\u76f4\u63a5\u6267\u884c\u3002<\/p>\n<p><strong>\u8f6c\u6362\u540e\u7684exe\u6587\u4ef6\u80fd\u5728\u5176\u4ed6\u8ba1\u7b97\u673a\u4e0a\u8fd0\u884c\u5417\uff1f<\/strong><br \/>\u662f\u7684\uff0c\u4f7f\u7528\u5de5\u5177\u5c06Python\u6587\u4ef6\u8f6c\u6362\u6210exe\u6587\u4ef6\u540e\uff0c\u751f\u6210\u7684\u53ef\u6267\u884c\u6587\u4ef6\u53ef\u4ee5\u5728\u6ca1\u6709Python\u73af\u5883\u7684\u5176\u4ed6\u8ba1\u7b97\u673a\u4e0a\u8fd0\u884c\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u786e\u4fdd\u5728\u6253\u5305\u8fc7\u7a0b\u4e2d\u5305\u542b\u6240\u6709\u5fc5\u8981\u7684\u4f9d\u8d56\u5e93\u548c\u8d44\u6e90\u6587\u4ef6\uff0c\u8fd9\u6837\u5728\u5176\u4ed6\u8ba1\u7b97\u673a\u4e0a\u8fd0\u884c\u65f6\u4e0d\u4f1a\u51fa\u73b0\u7f3a\u5931\u6587\u4ef6\u7684\u95ee\u9898\u3002<\/p>\n<p><strong>\u7f16\u8bd1\u540e\u7684exe\u6587\u4ef6\u662f\u5426\u4f1a\u5360\u7528\u5927\u91cf\u7a7a\u95f4\uff1f<\/strong><br \/>\u7f16\u8bd1\u540e\u7684exe\u6587\u4ef6\u901a\u5e38\u4f1a\u6bd4\u539f\u59cb\u7684Python\u811a\u672c\u5927\uff0c\u56e0\u4e3a\u5b83\u5305\u542b\u4e86Python\u89e3\u91ca\u5668\u548c\u6240\u6709\u4f9d\u8d56\u7684\u5e93\u6587\u4ef6\u3002\u6587\u4ef6\u5927\u5c0f\u4f1a\u6839\u636e\u9879\u76ee\u7684\u590d\u6742\u6027\u548c\u4f7f\u7528\u7684\u5e93\u800c\u6709\u6240\u4e0d\u540c\u3002\u5982\u679c\u9700\u8981\u51cf\u5c0fexe\u6587\u4ef6\u7684\u4f53\u79ef\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528PyInstaller\u7684&#8211;onefile\u9009\u9879\uff0c\u8fd9\u6837\u53ef\u4ee5\u5c06\u6240\u6709\u5185\u5bb9\u6253\u5305\u6210\u4e00\u4e2a\u5355\u72ec\u7684\u53ef\u6267\u884c\u6587\u4ef6\uff0c\u867d\u7136\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u53ef\u80fd\u4f1a\u7565\u5fae\u589e\u52a0\u542f\u52a8\u65f6\u95f4\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u5c06Python\u6587\u4ef6\u7f16\u8bd1\u6210exe\u6587\u4ef6\uff0c\u4e3b\u8981\u6709\u4ee5\u4e0b\u51e0\u79cd\u65b9\u6cd5\uff1a\u4f7f\u7528PyInstaller\u3001\u4f7f\u7528cx_Freeze\u3001 [&hellip;]","protected":false},"author":3,"featured_media":1086967,"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\/1086960"}],"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=1086960"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1086960\/revisions"}],"predecessor-version":[{"id":1086969,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1086960\/revisions\/1086969"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1086967"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1086960"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1086960"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1086960"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}