{"id":1171912,"date":"2025-01-15T16:41:40","date_gmt":"2025-01-15T08:41:40","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1171912.html"},"modified":"2025-01-15T16:41:42","modified_gmt":"2025-01-15T08:41:42","slug":"linux%e4%b8%ad%e5%a6%82%e4%bd%95%e7%bc%96%e8%af%91python%e6%96%87%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1171912.html","title":{"rendered":"linux\u4e2d\u5982\u4f55\u7f16\u8bd1python\u6587\u4ef6"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/26074135\/39669ba2-ca89-4825-8ac6-9b98174270aa.webp\" alt=\"linux\u4e2d\u5982\u4f55\u7f16\u8bd1python\u6587\u4ef6\" \/><\/p>\n<p><p> <strong>\u5728Linux\u4e2d\u7f16\u8bd1Python\u6587\u4ef6\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528PyInstaller\u3001cx_Freeze\u3001Nuitka\u7b49\u5de5\u5177\u3002<\/strong> \u8fd9\u4e9b\u5de5\u5177\u53ef\u4ee5\u5c06Python\u811a\u672c\u6253\u5305\u6210\u53ef\u6267\u884c\u6587\u4ef6\uff0c\u4ee5\u4fbf\u5728\u6ca1\u6709Python\u89e3\u91ca\u5668\u7684\u60c5\u51b5\u4e0b\u8fd0\u884c\u3002\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u5de5\u5177\u7684\u4f7f\u7528\u65b9\u6cd5\u548c\u6ce8\u610f\u4e8b\u9879\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u4f7f\u7528PyInstaller<\/h2>\n<\/p>\n<p><p>PyInstaller\u662f\u4e00\u4e2a\u975e\u5e38\u6d41\u884c\u7684Python\u6253\u5305\u5de5\u5177\uff0c\u5b83\u652f\u6301\u5927\u591a\u6570\u64cd\u4f5c\u7cfb\u7edf\u548cPython\u7248\u672c\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528PyInstaller\u7f16\u8bd1Python\u6587\u4ef6\u7684\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<p><h3>\u5b89\u88c5PyInstaller<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u4f7f\u7528pip\u5b89\u88c5PyInstaller\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pyinstaller<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u7f16\u8bd1Python\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u7f16\u8bd1Python\u6587\u4ef6\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>\u8be5\u547d\u4ee4\u5c06\u751f\u6210\u4e00\u4e2a\u5355\u72ec\u7684\u53ef\u6267\u884c\u6587\u4ef6\uff0c\u901a\u5e38\u4f4d\u4e8e<code>dist<\/code>\u76ee\u5f55\u4e2d\u3002<code>--onefile<\/code>\u9009\u9879\u8868\u793a\u5c06\u6240\u6709\u4f9d\u8d56\u9879\u6253\u5305\u5230\u4e00\u4e2a\u6587\u4ef6\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>\u81ea\u5b9a\u4e49\u7f16\u8bd1\u9009\u9879<\/h3>\n<\/p>\n<p><p>PyInstaller\u63d0\u4f9b\u4e86\u8bb8\u591a\u9009\u9879\u6765\u5b9a\u5236\u6253\u5305\u8fc7\u7a0b\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u6dfb\u52a0\u56fe\u6807\u6587\u4ef6\u3001\u6392\u9664\u7279\u5b9a\u6a21\u5757\u7b49\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pyinstaller --onefile --icon=your_icon.ico --exclude-module=tkinter your_script.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e8c\u3001\u4f7f\u7528cx_Freeze<\/h2>\n<\/p>\n<p><p>cx_Freeze\u662f\u53e6\u4e00\u4e2a\u6d41\u884c\u7684Python\u6253\u5305\u5de5\u5177\uff0c\u7279\u522b\u9002\u7528\u4e8e\u521b\u5efa\u8de8\u5e73\u53f0\u7684\u53ef\u6267\u884c\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h3>\u5b89\u88c5cx_Freeze<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u4f7f\u7528pip\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<p><h3>\u7f16\u5199setup\u811a\u672c<\/h3>\n<\/p>\n<p><p>\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a<code>setup.py<\/code>\u7684\u6587\u4ef6\uff0c\u5e76\u7f16\u5199\u4ee5\u4e0b\u5185\u5bb9\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;your_app_name&quot;,<\/p>\n<p>    version = &quot;1.0&quot;,<\/p>\n<p>    description = &quot;Your application description&quot;,<\/p>\n<p>    executables = [Executable(&quot;your_script.py&quot;)]<\/p>\n<p>)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u7f16\u8bd1Python\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\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>\u8be5\u547d\u4ee4\u5c06\u5728<code>build<\/code>\u76ee\u5f55\u4e2d\u751f\u6210\u53ef\u6267\u884c\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h2>\u4e09\u3001\u4f7f\u7528Nuitka<\/h2>\n<\/p>\n<p><p>Nuitka\u662f\u4e00\u4e2a\u5c06Python\u4ee3\u7801\u8f6c\u6362\u4e3aC++\u4ee3\u7801\u5e76\u8fdb\u884c\u7f16\u8bd1\u7684\u5de5\u5177\uff0c\u751f\u6210\u7684\u53ef\u6267\u884c\u6587\u4ef6\u6027\u80fd\u66f4\u9ad8\u3002<\/p>\n<\/p>\n<p><h3>\u5b89\u88c5Nuitka<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u4f7f\u7528pip\u5b89\u88c5Nuitka\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install nuitka<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u7f16\u8bd1Python\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u7f16\u8bd1Python\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">nuitka --standalone --onefile your_script.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8be5\u547d\u4ee4\u5c06\u751f\u6210\u4e00\u4e2a\u72ec\u7acb\u7684\u53ef\u6267\u884c\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h3>\u81ea\u5b9a\u4e49\u7f16\u8bd1\u9009\u9879<\/h3>\n<\/p>\n<p><p>Nuitka\u4e5f\u63d0\u4f9b\u4e86\u8bb8\u591a\u9009\u9879\u6765\u5b9a\u5236\u7f16\u8bd1\u8fc7\u7a0b\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u4f18\u5316\u4ee3\u7801\u3001\u6dfb\u52a0\u8c03\u8bd5\u4fe1\u606f\u7b49\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">nuitka --standalone --onefile --lto --debug your_script.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u56db\u3001\u6ce8\u610f\u4e8b\u9879<\/h2>\n<\/p>\n<p><h3>\u4f9d\u8d56\u7ba1\u7406<\/h3>\n<\/p>\n<p><p>\u5728\u7f16\u8bd1Python\u6587\u4ef6\u65f6\uff0c\u786e\u4fdd\u6240\u6709\u4f9d\u8d56\u9879\u90fd\u5df2\u6b63\u786e\u5b89\u88c5\u3002\u53ef\u4ee5\u4f7f\u7528<code>pip freeze &gt; requirements.txt<\/code>\u547d\u4ee4\u751f\u6210\u4f9d\u8d56\u9879\u5217\u8868\uff0c\u5e76\u4f7f\u7528<code>pip install -r requirements.txt<\/code>\u547d\u4ee4\u5b89\u88c5\u4f9d\u8d56\u9879\u3002<\/p>\n<\/p>\n<p><h3>\u5e73\u53f0\u517c\u5bb9\u6027<\/h3>\n<\/p>\n<p><p>\u4e0d\u540c\u7684\u6253\u5305\u5de5\u5177\u5728\u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\u4e0a\u7684\u8868\u73b0\u53ef\u80fd\u6709\u6240\u4e0d\u540c\u3002\u5728\u5f00\u53d1\u8fc7\u7a0b\u4e2d\uff0c\u5efa\u8bae\u5728\u76ee\u6807\u64cd\u4f5c\u7cfb\u7edf\u4e0a\u8fdb\u884c\u6d4b\u8bd5\uff0c\u4ee5\u786e\u4fdd\u53ef\u6267\u884c\u6587\u4ef6\u7684\u517c\u5bb9\u6027\u3002<\/p>\n<\/p>\n<p><h3>\u6587\u4ef6\u6743\u9650<\/h3>\n<\/p>\n<p><p>\u5728Linux\u4e2d\uff0c\u7f16\u8bd1\u540e\u7684\u53ef\u6267\u884c\u6587\u4ef6\u53ef\u80fd\u9700\u8981\u8bbe\u7f6e\u6267\u884c\u6743\u9650\u3002\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u8bbe\u7f6e\u6743\u9650\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">chmod +x your_executable_file<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u8c03\u8bd5\u4fe1\u606f<\/h3>\n<\/p>\n<p><p>\u5728\u6253\u5305\u8fc7\u7a0b\u4e2d\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u4e00\u4e9b\u9519\u8bef\u6216\u8b66\u544a\u4fe1\u606f\u3002\u9605\u8bfb\u8fd9\u4e9b\u4fe1\u606f\u53ef\u4ee5\u5e2e\u52a9\u60a8\u89e3\u51b3\u6f5c\u5728\u7684\u95ee\u9898\u3002\u4f8b\u5982\uff0c\u7f3a\u5c11\u67d0\u4e9b\u4f9d\u8d56\u9879\u6216\u6a21\u5757\u51b2\u7a81\u3002<\/p>\n<\/p>\n<p><h2>\u4e94\u3001\u603b\u7ed3<\/h2>\n<\/p>\n<p><p>\u5728Linux\u4e2d\u7f16\u8bd1Python\u6587\u4ef6\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c<strong>PyInstaller\u3001cx_Freeze\u3001Nuitka<\/strong>\u90fd\u662f\u975e\u5e38\u4f18\u79c0\u7684\u9009\u62e9\u3002\u6bcf\u79cd\u5de5\u5177\u90fd\u6709\u5176\u72ec\u7279\u7684\u4f18\u52bf\u548c\u4f7f\u7528\u573a\u666f\uff0c\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u5de5\u5177\u662f\u5173\u952e\u3002\u901a\u8fc7\u672c\u6587\u7684\u4ecb\u7ecd\uff0c\u60a8\u5e94\u8be5\u80fd\u591f\u987a\u5229\u5730\u5728Linux\u73af\u5883\u4e2d\u7f16\u8bd1Python\u6587\u4ef6\uff0c\u5e76\u751f\u6210\u53ef\u6267\u884c\u6587\u4ef6\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u9047\u5230\u95ee\u9898\u65f6\u4e5f\u53ef\u4ee5\u67e5\u9605\u76f8\u5173\u5de5\u5177\u7684\u5b98\u65b9\u6587\u6863\u548c\u793e\u533a\u8d44\u6e90\uff0c\u4ee5\u83b7\u5f97\u66f4\u591a\u7684\u5e2e\u52a9\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Linux\u4e2d\u7f16\u8bd1Python\u6587\u4ef6\u4e3a\u53ef\u6267\u884c\u6587\u4ef6\uff1f<\/strong><br \/>\u5728Linux\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>PyInstaller<\/code>\u6216<code>cx_Freeze<\/code>\u7b49\u5de5\u5177\u5c06Python\u6587\u4ef6\u7f16\u8bd1\u6210\u72ec\u7acb\u7684\u53ef\u6267\u884c\u6587\u4ef6\u3002\u5b89\u88c5\u8fd9\u4e9b\u5de5\u5177\u540e\uff0c\u53ef\u4ee5\u5728\u547d\u4ee4\u884c\u4e2d\u8fd0\u884c\u76f8\u5e94\u7684\u547d\u4ee4\uff0c\u5c06Python\u811a\u672c\u8f6c\u6362\u4e3a\u53ef\u6267\u884c\u6587\u4ef6\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>PyInstaller<\/code>\u7684\u547d\u4ee4\u4e3a<code>pyinstaller your_script.py<\/code>\uff0c\u751f\u6210\u7684\u53ef\u6267\u884c\u6587\u4ef6\u5c06\u5728<code>dist<\/code>\u76ee\u5f55\u4e2d\u3002<\/p>\n<p><strong>\u5728Linux\u4e0a\u7f16\u8bd1Python\u6587\u4ef6\u65f6\u9700\u8981\u6ce8\u610f\u54ea\u4e9b\u4f9d\u8d56\uff1f<\/strong><br \/>\u5728\u7f16\u8bd1Python\u6587\u4ef6\u4e4b\u524d\uff0c\u786e\u4fdd\u6240\u6709\u4f9d\u8d56\u7684\u5e93\u548c\u6a21\u5757\u90fd\u5df2\u5b89\u88c5\u5728\u7cfb\u7edf\u4e2d\u3002\u53ef\u4ee5\u901a\u8fc7<code>pip freeze<\/code>\u547d\u4ee4\u67e5\u770b\u5f53\u524d\u73af\u5883\u4e2d\u7684\u6240\u6709\u4f9d\u8d56\u9879\uff0c\u786e\u4fdd\u5728\u7f16\u8bd1\u524d\u89e3\u51b3\u4efb\u4f55\u7f3a\u5931\u7684\u5e93\uff0c\u4ee5\u907f\u514d\u8fd0\u884c\u65f6\u9519\u8bef\u3002<\/p>\n<p><strong>\u5982\u4f55\u8c03\u8bd5\u7f16\u8bd1\u540e\u7684Python\u53ef\u6267\u884c\u6587\u4ef6\uff1f<\/strong><br \/>\u7f16\u8bd1\u540e\u7684\u53ef\u6267\u884c\u6587\u4ef6\u53ef\u80fd\u4f1a\u51fa\u73b0\u95ee\u9898\uff0c\u56e0\u6b64\u8c03\u8bd5\u662f\u5fc5\u8981\u7684\u3002\u53ef\u4ee5\u5728\u7f16\u8bd1\u65f6\u4f7f\u7528<code>--debug<\/code>\u9009\u9879\uff0c\u8fd9\u6837\u5728\u8fd0\u884c\u53ef\u6267\u884c\u6587\u4ef6\u65f6\uff0c\u4f1a\u8f93\u51fa\u8c03\u8bd5\u4fe1\u606f\u3002\u6b64\u5916\uff0c\u8fd8\u53ef\u4ee5\u5728\u6e90\u4ee3\u7801\u4e2d\u6dfb\u52a0\u65e5\u5fd7\u8bb0\u5f55\uff0c\u5e2e\u52a9\u6392\u67e5\u95ee\u9898\uff0c\u786e\u4fdd\u4ee3\u7801\u5728\u7f16\u8bd1\u524d\u7ecf\u8fc7\u5145\u5206\u6d4b\u8bd5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Linux\u4e2d\u7f16\u8bd1Python\u6587\u4ef6\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528PyInstaller\u3001cx_Freeze\u3001Nuitk [&hellip;]","protected":false},"author":3,"featured_media":1171916,"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\/1171912"}],"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=1171912"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1171912\/revisions"}],"predecessor-version":[{"id":1171919,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1171912\/revisions\/1171919"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1171916"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1171912"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1171912"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1171912"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}