{"id":933974,"date":"2024-12-26T18:26:07","date_gmt":"2024-12-26T10:26:07","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/933974.html"},"modified":"2024-12-26T18:26:09","modified_gmt":"2024-12-26T10:26:09","slug":"python%e5%a6%82%e4%bd%95%e5%8f%98%e4%b8%baexe","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/933974.html","title":{"rendered":"python\u5982\u4f55\u53d8\u4e3aexe"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25071159\/c4ecfd02-8538-4a1b-ae30-8a04611a0435.webp\" alt=\"python\u5982\u4f55\u53d8\u4e3aexe\" \/><\/p>\n<p><p> \u5c06Python\u811a\u672c\u8f6c\u6362\u4e3a\u53ef\u6267\u884c\u6587\u4ef6\uff08.exe\uff09\u662f\u4e00\u4e2a\u5e38\u89c1\u7684\u9700\u6c42\uff0c\u7279\u522b\u662f\u5728\u9700\u8981\u5c06Python\u5e94\u7528\u7a0b\u5e8f\u5206\u53d1\u7ed9\u6ca1\u6709\u5b89\u88c5Python\u7684\u7528\u6237\u65f6\u3002<strong>\u5e38\u7528\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528PyInstaller\u3001cx_Freeze\u3001py2exe\u3001Nuitka<\/strong>\u3002\u8fd9\u4e9b\u5de5\u5177\u53ef\u4ee5\u5e2e\u52a9\u4f60\u5c06Python\u811a\u672c\u6253\u5305\u4e3a\u53ef\u6267\u884c\u6587\u4ef6\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5176\u4e2d\u4e00\u79cd\u65b9\u6cd5\u2014\u2014\u4f7f\u7528PyInstaller\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528PYINSTALLER<\/h3>\n<\/p>\n<p><h4>1\u3001\u5b89\u88c5PyInstaller<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u786e\u4fdd\u4f60\u5df2\u7ecf\u5b89\u88c5\u4e86PyInstaller\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528pip\u547d\u4ee4\u6765\u5b89\u88c5\u5b83\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pyinstaller<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u57fa\u672c\u4f7f\u7528\u65b9\u6cd5<\/h4>\n<\/p>\n<p><p>PyInstaller\u7684\u57fa\u672c\u7528\u6cd5\u975e\u5e38\u7b80\u5355\uff0c\u53ea\u9700\u5728\u547d\u4ee4\u884c\u4e2d\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\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a\u201cdist\u201d\u7684\u76ee\u5f55\uff0c\u5176\u4e2d\u5305\u542b\u4e00\u4e2a\u4e0e\u811a\u672c\u540c\u540d\u7684\u53ef\u6267\u884c\u6587\u4ef6\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0cPyInstaller\u5c06\u751f\u6210\u4e00\u4e2a\u5305\u542b\u6240\u6709\u4f9d\u8d56\u9879\u7684\u76ee\u5f55\u3002<\/p>\n<\/p>\n<p><h4>3\u3001\u4f7f\u7528\u5355\u6587\u4ef6\u6a21\u5f0f<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u5e0c\u671b\u751f\u6210\u4e00\u4e2a\u5355\u4e00\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>\u8fd9\u79cd\u65b9\u5f0f\u4f1a\u5c06\u6240\u6709\u7684\u4ee3\u7801\u548c\u4f9d\u8d56\u9879\u6253\u5305\u5230\u4e00\u4e2a\u72ec\u7acb\u7684\u53ef\u6267\u884c\u6587\u4ef6\u4e2d\uff0c\u4fbf\u4e8e\u5206\u53d1\u3002<\/p>\n<\/p>\n<p><h4>4\u3001\u6307\u5b9a\u56fe\u6807\u548c\u5176\u4ed6\u9009\u9879<\/h4>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u901a\u8fc7<code>--icon<\/code>\u9009\u9879\u6307\u5b9a\u53ef\u6267\u884c\u6587\u4ef6\u7684\u56fe\u6807\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>\u53e6\u5916\uff0cPyInstaller\u8fd8\u652f\u6301\u8bb8\u591a\u5176\u4ed6\u9009\u9879\uff0c\u6bd4\u5982\u6dfb\u52a0\u6570\u636e\u6587\u4ef6\u3001\u6392\u9664\u67d0\u4e9b\u6a21\u5757\u7b49\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u67e5\u770bPyInstaller\u7684\u5e2e\u52a9\u6587\u6863\u83b7\u53d6\u66f4\u591a\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528CX_FREEZE<\/h3>\n<\/p>\n<p><h4>1\u3001\u5b89\u88c5cx_Freeze<\/h4>\n<\/p>\n<p><p>cx_Freeze\u662f\u53e6\u4e00\u4e2a\u7528\u4e8e\u5c06Python\u811a\u672c\u8f6c\u6362\u4e3a\u53ef\u6267\u884c\u6587\u4ef6\u7684\u5de5\u5177\u3002\u9996\u5148\uff0c\u4f60\u9700\u8981\u5b89\u88c5\u5b83\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install cx_Freeze<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u521b\u5efasetup\u811a\u672c<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528cx_Freeze\u9700\u8981\u7f16\u5199\u4e00\u4e2asetup\u811a\u672c\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\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;MyApp&quot;,<\/p>\n<p>    version = &quot;0.1&quot;,<\/p>\n<p>    description = &quot;My Python Application&quot;,<\/p>\n<p>    executables = [Executable(&quot;your_script.py&quot;)]<\/p>\n<p>)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u6784\u5efa\u53ef\u6267\u884c\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u5728\u547d\u4ee4\u884c\u4e2d\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\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\u521b\u5efa\u4e00\u4e2a\u201cbuild\u201d\u76ee\u5f55\uff0c\u5176\u4e2d\u5305\u542b\u53ef\u6267\u884c\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528PY2EXE<\/h3>\n<\/p>\n<p><h4>1\u3001\u5b89\u88c5py2exe<\/h4>\n<\/p>\n<p><p>py2exe\u662f\u4e13\u95e8\u4e3aWindows\u73af\u5883\u8bbe\u8ba1\u7684\u5de5\u5177\u3002\u9996\u5148\u5b89\u88c5\u5b83\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install py2exe<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u521b\u5efasetup\u811a\u672c<\/h4>\n<\/p>\n<p><p>\u4e0ecx_Freeze\u7c7b\u4f3c\uff0c\u4f60\u9700\u8981\u521b\u5efa\u4e00\u4e2asetup\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;your_script.py&#39;])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u751f\u6210\u53ef\u6267\u884c\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\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\u5c06\u5728\u201cdist\u201d\u76ee\u5f55\u4e2d\u751f\u6210\u53ef\u6267\u884c\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528NUITKA<\/h3>\n<\/p>\n<p><h4>1\u3001\u5b89\u88c5Nuitka<\/h4>\n<\/p>\n<p><p>Nuitka\u662f\u4e00\u4e2aPython\u7f16\u8bd1\u5668\uff0c\u53ef\u4ee5\u5c06Python\u4ee3\u7801\u7f16\u8bd1\u4e3aC\u4ee3\u7801\uff0c\u7136\u540e\u751f\u6210\u53ef\u6267\u884c\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install nuitka<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u7f16\u8bd1\u811a\u672c<\/h4>\n<\/p>\n<p><p>\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">nuitka --standalone --mingw64 --output-dir=out your_script.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u751f\u6210\u4e00\u4e2a\u5305\u542b\u53ef\u6267\u884c\u6587\u4ef6\u7684\u76ee\u5f55\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u6ce8\u610f\u4e8b\u9879<\/h3>\n<\/p>\n<p><h4>1\u3001\u517c\u5bb9\u6027\u95ee\u9898<\/h4>\n<\/p>\n<p><p>\u4e0d\u540c\u5de5\u5177\u5bf9Python\u7248\u672c\u548c\u7b2c\u4e09\u65b9\u5e93\u7684\u652f\u6301\u53ef\u80fd\u4f1a\u6709\u6240\u4e0d\u540c\uff0c\u56e0\u6b64\u5728\u9009\u62e9\u5de5\u5177\u65f6\u9700\u8981\u6ce8\u610f\u517c\u5bb9\u6027\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u6587\u4ef6\u5927\u5c0f<\/h4>\n<\/p>\n<p><p>\u751f\u6210\u7684\u53ef\u6267\u884c\u6587\u4ef6\u53ef\u80fd\u4f1a\u6bd4\u8f83\u5927\uff0c\u8fd9\u662f\u56e0\u4e3a\u6240\u6709\u7684\u4f9d\u8d56\u9879\u90fd\u88ab\u6253\u5305\u5728\u4e00\u8d77\u4e86\u3002\u5982\u679c\u6587\u4ef6\u5927\u5c0f\u662f\u4e2a\u95ee\u9898\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u5de5\u5177\u7684\u9009\u9879\u6765\u6392\u9664\u4e0d\u5fc5\u8981\u7684\u6a21\u5757\u3002<\/p>\n<\/p>\n<p><h4>3\u3001\u8c03\u8bd5\u4fe1\u606f<\/h4>\n<\/p>\n<p><p>\u5728\u751f\u6210\u53ef\u6267\u884c\u6587\u4ef6\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u9009\u9879\u6765\u63a7\u5236\u662f\u5426\u5305\u542b\u8c03\u8bd5\u4fe1\u606f\u3002\u8c03\u8bd5\u4fe1\u606f\u53ef\u4ee5\u5e2e\u52a9\u5b9a\u4f4d\u95ee\u9898\uff0c\u4f46\u540c\u65f6\u4e5f\u4f1a\u589e\u5927\u6587\u4ef6\u5927\u5c0f\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u65b9\u6cd5\uff0c\u4f60\u53ef\u4ee5\u5c06Python\u811a\u672c\u8f6c\u6362\u4e3a\u53ef\u6267\u884c\u6587\u4ef6\uff0c\u4fbf\u4e8e\u5206\u53d1\u548c\u4f7f\u7528\u3002\u65e0\u8bba\u662fPyInstaller\u3001cx_Freeze\u3001py2exe\u8fd8\u662fNuitka\uff0c\u6bcf\u79cd\u5de5\u5177\u90fd\u6709\u5176\u72ec\u7279\u7684\u4f18\u52bf\u548c\u9002\u7528\u573a\u666f\uff0c\u9009\u62e9\u9002\u5408\u81ea\u5df1\u7684\u5de5\u5177\u662f\u5173\u952e\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\u53ef\u6267\u884c\u6587\u4ef6\uff08.exe\uff09\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e9b\u6d41\u884c\u7684\u5de5\u5177\uff0c\u6bd4\u5982PyInstaller\u3001cx_Freeze\u6216py2exe\u3002\u4ee5PyInstaller\u4e3a\u4f8b\uff0c\u60a8\u53ea\u9700\u5728\u547d\u4ee4\u884c\u4e2d\u8fd0\u884c<code>pyinstaller your_script.py<\/code>\uff0c\u5b83\u5c06\u751f\u6210\u4e00\u4e2a\u5305\u542b\u6240\u6709\u4f9d\u8d56\u9879\u7684.exe\u6587\u4ef6\uff0c\u901a\u5e38\u5728\u201cdist\u201d\u6587\u4ef6\u5939\u4e2d\u627e\u5230\u3002<\/p>\n<p><strong>\u8f6c\u6362\u540e\u7684exe\u6587\u4ef6\u5728\u5176\u4ed6\u7535\u8111\u4e0a\u80fd\u5426\u8fd0\u884c\uff1f<\/strong><br \/>\u662f\u7684\uff0c\u8f6c\u6362\u540e\u7684.exe\u6587\u4ef6\u53ef\u4ee5\u5728\u6ca1\u6709Python\u73af\u5883\u7684\u5176\u4ed6\u7535\u8111\u4e0a\u8fd0\u884c\uff0c\u4f46\u9700\u8981\u786e\u4fdd\u6240\u6709\u4f9d\u8d56\u7684\u5e93\u548c\u8d44\u6e90\u90fd\u5df2\u5305\u542b\u5728\u6253\u5305\u6587\u4ef6\u4e2d\u3002\u4f7f\u7528PyInstaller\u65f6\uff0c\u901a\u5e38\u53ef\u4ee5\u901a\u8fc7\u6dfb\u52a0\u4e00\u4e9b\u53c2\u6570\u6765\u786e\u4fdd\u6240\u6709\u5fc5\u8981\u7684\u6587\u4ef6\u90fd\u88ab\u6253\u5305\u3002<\/p>\n<p><strong>\u5728\u8f6c\u6362\u8fc7\u7a0b\u4e2d\u9047\u5230\u9519\u8bef\u8be5\u5982\u4f55\u89e3\u51b3\uff1f<\/strong><br \/>\u5982\u679c\u5728\u4f7f\u7528PyInstaller\u6216\u5176\u4ed6\u5de5\u5177\u65f6\u9047\u5230\u9519\u8bef\uff0c\u5efa\u8bae\u4ed4\u7ec6\u67e5\u770b\u9519\u8bef\u4fe1\u606f\uff0c\u67e5\u627e\u7f3a\u5931\u7684\u6a21\u5757\u6216\u4f9d\u8d56\u9879\u3002\u8fd8\u53ef\u4ee5\u5c1d\u8bd5\u66f4\u65b0Python\u53ca\u5176\u5e93\u5230\u6700\u65b0\u7248\u672c\uff0c\u6216\u67e5\u9605\u76f8\u5173\u6587\u6863\u548c\u793e\u533a\u652f\u6301\uff0c\u83b7\u53d6\u9488\u5bf9\u7279\u5b9a\u9519\u8bef\u7684\u89e3\u51b3\u65b9\u6848\u3002\u6b64\u5916\uff0c\u68c0\u67e5\u60a8\u7684\u811a\u672c\u662f\u5426\u5b58\u5728\u8bed\u6cd5\u9519\u8bef\u6216\u8fd0\u884c\u65f6\u9519\u8bef\u4e5f\u662f\u975e\u5e38\u91cd\u8981\u7684\u6b65\u9aa4\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5c06Python\u811a\u672c\u8f6c\u6362\u4e3a\u53ef\u6267\u884c\u6587\u4ef6\uff08.exe\uff09\u662f\u4e00\u4e2a\u5e38\u89c1\u7684\u9700\u6c42\uff0c\u7279\u522b\u662f\u5728\u9700\u8981\u5c06Python\u5e94\u7528\u7a0b\u5e8f\u5206\u53d1\u7ed9\u6ca1\u6709\u5b89 [&hellip;]","protected":false},"author":3,"featured_media":933977,"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\/933974"}],"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=933974"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/933974\/revisions"}],"predecessor-version":[{"id":933979,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/933974\/revisions\/933979"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/933977"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=933974"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=933974"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=933974"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}