{"id":1164772,"date":"2025-01-15T15:11:57","date_gmt":"2025-01-15T07:11:57","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1164772.html"},"modified":"2025-01-15T15:12:00","modified_gmt":"2025-01-15T07:12:00","slug":"%e5%a6%82%e4%bd%95%e6%8a%8apython%e6%96%87%e4%bb%b6%e5%86%99%e6%88%90exe","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1164772.html","title":{"rendered":"\u5982\u4f55\u628apython\u6587\u4ef6\u5199\u6210exe"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25205258\/26f940d6-1e65-4ef5-a99c-f8d75e1f07b3.webp\" alt=\"\u5982\u4f55\u628apython\u6587\u4ef6\u5199\u6210exe\" \/><\/p>\n<p><p> <strong>\u8981\u5c06Python\u6587\u4ef6\u8f6c\u6362\u6210exe\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528\u5de5\u5177\u5982PyInstaller\u3001cx_Freeze\u548cpy2exe\u3002\u8fd9\u4e9b\u5de5\u5177\u90fd\u80fd\u5c06Python\u811a\u672c\u6253\u5305\u6210\u72ec\u7acb\u7684\u53ef\u6267\u884c\u6587\u4ef6\uff0c\u65b9\u4fbf\u5728\u6ca1\u6709Python\u73af\u5883\u7684\u8ba1\u7b97\u673a\u4e0a\u8fd0\u884c\u3002<\/strong> \u8fd9\u91cc\u6211\u4eec\u5c06\u8be6\u7ec6\u63cf\u8ff0\u5982\u4f55\u4f7f\u7528PyInstaller\u8fd9\u4e2a\u5de5\u5177\u6765\u5b8c\u6210\u8fd9\u4e00\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><p><strong>PyInstaller<\/strong> \u662f\u4e00\u4e2a\u975e\u5e38\u6d41\u884c\u7684\u5de5\u5177\uff0c\u56e0\u4e3a\u5b83\u6613\u4e8e\u4f7f\u7528\uff0c\u5e76\u4e14\u652f\u6301\u591a\u79cd\u64cd\u4f5c\u7cfb\u7edf\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528PyInstaller\u5c06Python\u6587\u4ef6\u8f6c\u6362\u4e3aexe\u6587\u4ef6\u7684\u8be6\u7ec6\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u5b89\u88c5PyInstaller<\/p>\n<\/p>\n<p><p>\u5728\u5f00\u59cb\u8f6c\u6362\u4e4b\u524d\uff0c\u4f60\u9700\u8981\u786e\u4fdd\u5df2\u7ecf\u5b89\u88c5\u4e86PyInstaller\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7Python\u7684\u5305\u7ba1\u7406\u5de5\u5177pip\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><p>\u4e8c\u3001\u7f16\u5199Python\u811a\u672c<\/p>\n<\/p>\n<p><p>\u5047\u8bbe\u4f60\u5df2\u7ecf\u6709\u4e00\u4e2aPython\u811a\u672c\u6587\u4ef6\uff0c\u4f8b\u5982 <code>my_script.py<\/code>\u3002\u8fd9\u4e2a\u811a\u672c\u53ef\u4ee5\u662f\u4efb\u4f55Python\u7a0b\u5e8f\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528PyInstaller\u6253\u5305<\/p>\n<\/p>\n<p><p>\u6253\u5f00\u547d\u4ee4\u63d0\u793a\u7b26\u6216\u7ec8\u7aef\uff0c\u5e76\u5bfc\u822a\u5230\u4f60\u7684Python\u811a\u672c\u6240\u5728\u7684\u76ee\u5f55\u3002\u7136\u540e\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\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><strong>&#8211;onefile<\/strong> \u53c2\u6570\u8868\u793a\u5c06\u6240\u6709\u6587\u4ef6\u6253\u5305\u6210\u4e00\u4e2a\u72ec\u7acb\u7684\u53ef\u6267\u884c\u6587\u4ef6\u3002\u8fd0\u884c\u8be5\u547d\u4ee4\u540e\uff0cPyInstaller\u4f1a\u5728\u5f53\u524d\u76ee\u5f55\u4e0b\u751f\u6210\u4e00\u4e2a <code>dist<\/code> \u6587\u4ef6\u5939\uff0c\u5176\u4e2d\u5305\u542b\u751f\u6210\u7684exe\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u5b9a\u5236\u6253\u5305\u8fc7\u7a0b<\/p>\n<\/p>\n<p><p>PyInstaller\u63d0\u4f9b\u4e86\u591a\u79cd\u9009\u9879\uff0c\u5141\u8bb8\u4f60\u5bf9\u6253\u5305\u8fc7\u7a0b\u8fdb\u884c\u5b9a\u5236\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u9009\u9879\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u56fe\u6807\u6587\u4ef6<\/strong>\uff1a\u4f60\u53ef\u4ee5\u6307\u5b9a\u4e00\u4e2a\u56fe\u6807\u6587\u4ef6\uff08.ico\uff09\u6765\u66ff\u6362\u9ed8\u8ba4\u7684\u56fe\u6807\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pyinstaller --onefile --icon=your_icon.ico my_script.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\u4f60\u53ef\u4ee5\u9690\u85cf\u63a7\u5236\u53f0\u7a97\u53e3\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pyinstaller --onefile --windowed my_script.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6dfb\u52a0\u6570\u636e\u6587\u4ef6<\/strong>\uff1a\u5982\u679c\u4f60\u7684\u7a0b\u5e8f\u4f9d\u8d56\u4e8e\u67d0\u4e9b\u6570\u636e\u6587\u4ef6\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 <code>--add-data<\/code> \u9009\u9879\u6765\u5305\u542b\u5b83\u4eec\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pyinstaller --onefile --add-data &#39;data_file.txt;.&#39; my_script.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e94\u3001\u8c03\u8bd5\u548c\u4f18\u5316<\/p>\n<\/p>\n<p><p>\u5728\u751f\u6210exe\u6587\u4ef6\u540e\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u8fdb\u884c\u6d4b\u8bd5\u548c\u4f18\u5316\u3002\u5982\u679c\u4f60\u7684\u7a0b\u5e8f\u5728\u67d0\u4e9b\u8ba1\u7b97\u673a\u4e0a\u65e0\u6cd5\u8fd0\u884c\uff0c\u53ef\u80fd\u662f\u56e0\u4e3a\u7f3a\u5c11\u67d0\u4e9b\u4f9d\u8d56\u9879\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528PyInstaller\u7684 <code>--debug<\/code> \u9009\u9879\u6765\u751f\u6210\u5e26\u6709\u8c03\u8bd5\u4fe1\u606f\u7684\u53ef\u6267\u884c\u6587\u4ef6\uff0c\u4ee5\u4fbf\u66f4\u5bb9\u6613\u627e\u5230\u95ee\u9898\u6240\u5728\u3002<\/p>\n<\/p>\n<p><p>\u516d\u3001\u53d1\u5e03\u548c\u5206\u53d1<\/p>\n<\/p>\n<p><p>\u4e00\u65e6\u4f60\u751f\u6210\u4e86exe\u6587\u4ef6\uff0c\u5c31\u53ef\u4ee5\u5c06\u5b83\u5206\u53d1\u7ed9\u5176\u4ed6\u7528\u6237\u3002\u4f60\u53ef\u4ee5\u5c06exe\u6587\u4ef6\u6253\u5305\u6210\u5b89\u88c5\u5305\uff0c\u6216\u8005\u76f4\u63a5\u5206\u53d1\u7ed9\u7528\u6237\u3002<\/p>\n<\/p>\n<p><p><strong>\u603b\u7ed3<\/strong><\/p>\n<\/p>\n<p><p>\u5c06Python\u6587\u4ef6\u8f6c\u6362\u6210exe\u6587\u4ef6\u7684\u8fc7\u7a0b\u4e3b\u8981\u5305\u62ec\u5b89\u88c5PyInstaller\u3001\u7f16\u5199Python\u811a\u672c\u3001\u4f7f\u7528PyInstaller\u6253\u5305\u3001\u5b9a\u5236\u6253\u5305\u8fc7\u7a0b\u3001\u8c03\u8bd5\u548c\u4f18\u5316\u4ee5\u53ca\u53d1\u5e03\u548c\u5206\u53d1\u3002\u901a\u8fc7PyInstaller\u7684\u5404\u79cd\u9009\u9879\uff0c\u4f60\u53ef\u4ee5\u6839\u636e\u81ea\u5df1\u7684\u9700\u6c42\u5bf9\u6253\u5305\u8fc7\u7a0b\u8fdb\u884c\u5b9a\u5236\uff0c\u4ee5\u751f\u6210\u7b26\u5408\u8981\u6c42\u7684\u53ef\u6267\u884c\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u63a2\u8ba8\u6bcf\u4e00\u4e2a\u6b65\u9aa4\u548c\u76f8\u5173\u7684\u7ec6\u8282\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u5b89\u88c5PyInstaller<\/h2>\n<\/p>\n<p><p>PyInstaller\u662f\u4e00\u4e2a\u529f\u80fd\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u80fd\u591f\u5c06Python\u811a\u672c\u6253\u5305\u6210\u72ec\u7acb\u7684\u53ef\u6267\u884c\u6587\u4ef6\uff0c\u9002\u7528\u4e8eWindows\u3001Mac\u548cLinux\u7b49\u591a\u4e2a\u64cd\u4f5c\u7cfb\u7edf\u3002\u5b89\u88c5PyInstaller\u975e\u5e38\u7b80\u5355\uff0c\u53ea\u9700\u901a\u8fc7pip\u547d\u4ee4\u5373\u53ef\u5b8c\u6210\u3002<\/p>\n<\/p>\n<p><h3>1.1 \u5b89\u88c5PyInstaller<\/h3>\n<\/p>\n<p><p>\u5728\u547d\u4ee4\u63d0\u793a\u7b26\u6216\u7ec8\u7aef\u4e2d\u8fd0\u884c\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<p><p>\u6b64\u547d\u4ee4\u4f1a\u4ecePython\u5305\u7ba1\u7406\u5668\u4e2d\u4e0b\u8f7d\u5e76\u5b89\u88c5PyInstaller\u3002\u5982\u679c\u4f60\u5df2\u7ecf\u5b89\u88c5\u4e86PyInstaller\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u6765\u66f4\u65b0\u5230\u6700\u65b0\u7248\u672c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install --upgrade pyinstaller<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>1.2 \u9a8c\u8bc1\u5b89\u88c5<\/h3>\n<\/p>\n<p><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\u6765\u9a8c\u8bc1PyInstaller\u662f\u5426\u5b89\u88c5\u6210\u529f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pyinstaller --version<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5982\u679c\u6b63\u786e\u5b89\u88c5\uff0c\u4f60\u4f1a\u770b\u5230PyInstaller\u7684\u7248\u672c\u53f7\u8f93\u51fa\u3002<\/p>\n<\/p>\n<p><h2>\u4e8c\u3001\u7f16\u5199Python\u811a\u672c<\/h2>\n<\/p>\n<p><p>\u5728\u5c06Python\u6587\u4ef6\u8f6c\u6362\u4e3aexe\u6587\u4ef6\u4e4b\u524d\uff0c\u4f60\u9700\u8981\u6709\u4e00\u4e2aPython\u811a\u672c\u3002\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u7b80\u5355\u7684Python\u811a\u672c <code>my_script.py<\/code>\uff0c\u5176\u5185\u5bb9\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<p>def m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n():<\/p>\n<p>    print(&quot;Hello, World!&quot;)<\/p>\n<p>    print(&quot;Python version:&quot;, sys.version)<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u811a\u672c\u4f1a\u6253\u5370 &quot;Hello, World!&quot; \u548c\u5f53\u524dPython\u7248\u672c\u3002<\/p>\n<\/p>\n<p><h2>\u4e09\u3001\u4f7f\u7528PyInstaller\u6253\u5305<\/h2>\n<\/p>\n<p><p>\u6709\u4e86Python\u811a\u672c\u4e4b\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528PyInstaller\u5c06\u5176\u6253\u5305\u6210exe\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h3>3.1 \u57fa\u672c\u6253\u5305<\/h3>\n<\/p>\n<p><p>\u5728\u547d\u4ee4\u63d0\u793a\u7b26\u6216\u7ec8\u7aef\u4e2d\u5bfc\u822a\u5230\u4f60\u7684Python\u811a\u672c\u6240\u5728\u7684\u76ee\u5f55\uff0c\u7136\u540e\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\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>\u6b64\u547d\u4ee4\u4f1a\u5728\u5f53\u524d\u76ee\u5f55\u4e0b\u751f\u6210\u4e00\u4e2a <code>dist<\/code> \u6587\u4ef6\u5939\uff0c\u5176\u4e2d\u5305\u542b\u751f\u6210\u7684exe\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h3>3.2 \u89e3\u91ca\u8f93\u51fa\u6587\u4ef6\u5939\u7ed3\u6784<\/h3>\n<\/p>\n<p><p>\u6253\u5305\u5b8c\u6210\u540e\uff0cPyInstaller\u4f1a\u751f\u6210\u51e0\u4e2a\u6587\u4ef6\u5939\u548c\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<ul>\n<li><code>dist<\/code> \u6587\u4ef6\u5939\uff1a\u5305\u542b\u751f\u6210\u7684exe\u6587\u4ef6\u3002<\/li>\n<li><code>build<\/code> \u6587\u4ef6\u5939\uff1a\u5305\u542b\u4e34\u65f6\u6587\u4ef6\u548c\u6784\u5efa\u8fc7\u7a0b\u4e2d\u7684\u4e2d\u95f4\u6587\u4ef6\u3002<\/li>\n<li><code>my_script.spec<\/code> \u6587\u4ef6\uff1aPyInstaller\u7684\u914d\u7f6e\u6587\u4ef6\uff0c\u53ef\u4ee5\u7528\u6765\u5b9a\u5236\u6253\u5305\u8fc7\u7a0b\u3002<\/li>\n<\/ul>\n<p><h3>3.3 \u5b9a\u5236\u6253\u5305\u8fc7\u7a0b<\/h3>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u7f16\u8f91 <code>my_script.spec<\/code> \u6587\u4ef6\u6765\u5b9a\u5236\u6253\u5305\u8fc7\u7a0b\u3002\u4f8b\u5982\uff0c\u4f60\u53ef\u4ee5\u6dfb\u52a0\u989d\u5916\u7684\u6570\u636e\u6587\u4ef6\u3001\u66f4\u6539\u56fe\u6807\u3001\u8bbe\u7f6e\u4e0d\u540c\u7684\u6253\u5305\u9009\u9879\u7b49\u3002<\/p>\n<\/p>\n<p><h2>\u56db\u3001\u5b9a\u5236\u6253\u5305\u8fc7\u7a0b<\/h2>\n<\/p>\n<p><p>PyInstaller\u63d0\u4f9b\u4e86\u591a\u79cd\u9009\u9879\uff0c\u5141\u8bb8\u4f60\u5bf9\u6253\u5305\u8fc7\u7a0b\u8fdb\u884c\u5b9a\u5236\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u9009\u9879\uff1a<\/p>\n<\/p>\n<p><h3>4.1 \u6307\u5b9a\u56fe\u6807\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u901a\u8fc7 <code>--icon<\/code> \u9009\u9879\u6765\u6307\u5b9a\u4e00\u4e2a\u56fe\u6807\u6587\u4ef6\uff08.ico\uff09\u6765\u66ff\u6362\u9ed8\u8ba4\u7684\u56fe\u6807\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pyinstaller --onefile --icon=your_icon.ico my_script.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>4.2 \u9690\u85cf\u63a7\u5236\u53f0\u7a97\u53e3<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u7684\u5e94\u7528\u7a0b\u5e8f\u662f\u4e00\u4e2aGUI\u5e94\u7528\u7a0b\u5e8f\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 <code>--windowed<\/code> \u9009\u9879\u6765\u9690\u85cf\u63a7\u5236\u53f0\u7a97\u53e3\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pyinstaller --onefile --windowed my_script.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>4.3 \u6dfb\u52a0\u6570\u636e\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u7684\u7a0b\u5e8f\u4f9d\u8d56\u4e8e\u67d0\u4e9b\u6570\u636e\u6587\u4ef6\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 <code>--add-data<\/code> \u9009\u9879\u6765\u5305\u542b\u5b83\u4eec\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pyinstaller --onefile --add-data &#39;data_file.txt;.&#39; my_script.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u6b64\u547d\u4ee4\u4e2d\uff0c<code>data_file.txt<\/code> \u662f\u4f60\u8981\u5305\u542b\u7684\u6570\u636e\u6587\u4ef6\uff0c<code>.<\/code> \u8868\u793a\u5c06\u6587\u4ef6\u653e\u7f6e\u5728exe\u6587\u4ef6\u7684\u5f53\u524d\u76ee\u5f55\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>4.4 \u7f16\u8f91.spec\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u4f60\u8fd8\u53ef\u4ee5\u7f16\u8f91\u751f\u6210\u7684 <code>.spec<\/code> \u6587\u4ef6\u6765\u8fdb\u4e00\u6b65\u5b9a\u5236\u6253\u5305\u8fc7\u7a0b\u3002\u6253\u5f00 <code>my_script.spec<\/code> \u6587\u4ef6\uff0c\u4f60\u4f1a\u770b\u5230\u7c7b\u4f3c\u4ee5\u4e0b\u7684\u5185\u5bb9\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># -*- mode: python ; coding: utf-8 -*-<\/p>\n<p>block_cipher = None<\/p>\n<p>a = Analysis(<\/p>\n<p>    [&#39;my_script.py&#39;],<\/p>\n<p>    pathex=[],<\/p>\n<p>    binaries=[],<\/p>\n<p>    datas=[],<\/p>\n<p>    hiddenimports=[],<\/p>\n<p>    hookspath=[],<\/p>\n<p>    runtime_hooks=[],<\/p>\n<p>    excludes=[],<\/p>\n<p>    win_no_prefer_redirects=False,<\/p>\n<p>    win_private_assemblies=False,<\/p>\n<p>    cipher=block_cipher,<\/p>\n<p>)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u5728 <code>datas<\/code> \u53c2\u6570\u4e2d\u6dfb\u52a0\u6570\u636e\u6587\u4ef6\uff0c\u5728 <code>binaries<\/code> \u53c2\u6570\u4e2d\u6dfb\u52a0\u989d\u5916\u7684\u4e8c\u8fdb\u5236\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h2>\u4e94\u3001\u8c03\u8bd5\u548c\u4f18\u5316<\/h2>\n<\/p>\n<p><p>\u5728\u751f\u6210exe\u6587\u4ef6\u540e\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u8fdb\u884c\u6d4b\u8bd5\u548c\u4f18\u5316\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u8c03\u8bd5\u548c\u4f18\u5316\u7684\u5efa\u8bae\uff1a<\/p>\n<\/p>\n<p><h3>5.1 \u4f7f\u7528\u8c03\u8bd5\u9009\u9879<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u7684\u7a0b\u5e8f\u5728\u67d0\u4e9b\u8ba1\u7b97\u673a\u4e0a\u65e0\u6cd5\u8fd0\u884c\uff0c\u53ef\u80fd\u662f\u56e0\u4e3a\u7f3a\u5c11\u67d0\u4e9b\u4f9d\u8d56\u9879\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528PyInstaller\u7684 <code>--debug<\/code> \u9009\u9879\u6765\u751f\u6210\u5e26\u6709\u8c03\u8bd5\u4fe1\u606f\u7684\u53ef\u6267\u884c\u6587\u4ef6\uff0c\u4ee5\u4fbf\u66f4\u5bb9\u6613\u627e\u5230\u95ee\u9898\u6240\u5728\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pyinstaller --onefile --debug my_script.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>5.2 \u5206\u6790\u4f9d\u8d56\u9879<\/h3>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u4f7f\u7528 <code>--hidden-import<\/code> \u9009\u9879\u6765\u663e\u5f0f\u5730\u5305\u542b\u67d0\u4e9b\u4f9d\u8d56\u9879\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pyinstaller --onefile --hidden-import=module_name my_script.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>5.3 \u4f18\u5316\u53ef\u6267\u884c\u6587\u4ef6\u5927\u5c0f<\/h3>\n<\/p>\n<p><p>\u751f\u6210\u7684exe\u6587\u4ef6\u53ef\u80fd\u4f1a\u975e\u5e38\u5927\uff0c\u56e0\u4e3a\u5b83\u5305\u542b\u4e86Python\u89e3\u91ca\u5668\u548c\u6240\u6709\u4f9d\u8d56\u9879\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\u6765\u4f18\u5316\u53ef\u6267\u884c\u6587\u4ef6\u7684\u5927\u5c0f\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p>\u4f7f\u7528 <code>--clean<\/code> \u9009\u9879\u6765\u6e05\u7406\u4e34\u65f6\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pyinstaller --onefile --clean my_script.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p>\u4f7f\u7528 <code>--strip<\/code> \u9009\u9879\u6765\u79fb\u9664\u8c03\u8bd5\u4fe1\u606f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pyinstaller --onefile --strip my_script.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><h2>\u516d\u3001\u53d1\u5e03\u548c\u5206\u53d1<\/h2>\n<\/p>\n<p><p>\u4e00\u65e6\u4f60\u751f\u6210\u4e86exe\u6587\u4ef6\uff0c\u5c31\u53ef\u4ee5\u5c06\u5b83\u5206\u53d1\u7ed9\u5176\u4ed6\u7528\u6237\u3002\u4f60\u53ef\u4ee5\u5c06exe\u6587\u4ef6\u6253\u5305\u6210\u5b89\u88c5\u5305\uff0c\u6216\u8005\u76f4\u63a5\u5206\u53d1\u7ed9\u7528\u6237\u3002<\/p>\n<\/p>\n<p><h3>6.1 \u6253\u5305\u6210\u5b89\u88c5\u5305<\/h3>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u4f7f\u7528\u5de5\u5177\u5982 Inno Setup\u3001NSIS \u6216\u8005 PyInstaller \u81ea\u5e26\u7684 <code>--onefile<\/code> \u9009\u9879\u6765\u521b\u5efa\u5b89\u88c5\u5305\u3002\u8fd9\u4e9b\u5de5\u5177\u5141\u8bb8\u4f60\u521b\u5efa\u4e00\u4e2a\u5b89\u88c5\u7a0b\u5e8f\uff0c\u7528\u6237\u53ef\u4ee5\u901a\u8fc7\u8fd0\u884c\u5b89\u88c5\u7a0b\u5e8f\u5c06\u4f60\u7684\u5e94\u7528\u7a0b\u5e8f\u5b89\u88c5\u5230\u4ed6\u4eec\u7684\u8ba1\u7b97\u673a\u4e0a\u3002<\/p>\n<\/p>\n<p><h3>6.2 \u76f4\u63a5\u5206\u53d1<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u7684\u5e94\u7528\u7a0b\u5e8f\u4e0d\u9700\u8981\u590d\u6742\u7684\u5b89\u88c5\u8fc7\u7a0b\uff0c\u4f60\u53ef\u4ee5\u76f4\u63a5\u5206\u53d1\u751f\u6210\u7684exe\u6587\u4ef6\u3002\u4f60\u53ef\u4ee5\u5c06exe\u6587\u4ef6\u4e0a\u4f20\u5230\u4e91\u5b58\u50a8\u670d\u52a1\uff08\u5982Google Drive\u3001Dropbox\u7b49\uff09\u5e76\u5206\u4eab\u4e0b\u8f7d\u94fe\u63a5\uff0c\u6216\u8005\u901a\u8fc7\u7535\u5b50\u90ae\u4ef6\u76f4\u63a5\u53d1\u9001\u7ed9\u7528\u6237\u3002<\/p>\n<\/p>\n<p><h2>\u4e03\u3001\u603b\u7ed3<\/h2>\n<\/p>\n<p><p>\u901a\u8fc7\u4f7f\u7528PyInstaller\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u5c06Python\u811a\u672c\u6253\u5305\u6210\u72ec\u7acb\u7684\u53ef\u6267\u884c\u6587\u4ef6\u3002\u5728\u6253\u5305\u8fc7\u7a0b\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u6839\u636e\u9700\u6c42\u8fdb\u884c\u5404\u79cd\u5b9a\u5236\uff0c\u5982\u66f4\u6539\u56fe\u6807\u3001\u9690\u85cf\u63a7\u5236\u53f0\u7a97\u53e3\u3001\u6dfb\u52a0\u6570\u636e\u6587\u4ef6\u7b49\u3002\u6b64\u5916\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u901a\u8fc7\u8c03\u8bd5\u548c\u4f18\u5316\u6765\u786e\u4fdd\u751f\u6210\u7684\u53ef\u6267\u884c\u6587\u4ef6\u5728\u4e0d\u540c\u73af\u5883\u4e2d\u6b63\u5e38\u8fd0\u884c\u3002<\/p>\n<\/p>\n<p><p>\u5c06Python\u6587\u4ef6\u8f6c\u6362\u6210exe\u6587\u4ef6\u7684\u8fc7\u7a0b\u4e3b\u8981\u5305\u62ec\u4ee5\u4e0b\u51e0\u4e2a\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u5b89\u88c5PyInstaller<\/li>\n<li>\u7f16\u5199Python\u811a\u672c<\/li>\n<li>\u4f7f\u7528PyInstaller\u6253\u5305<\/li>\n<li>\u5b9a\u5236\u6253\u5305\u8fc7\u7a0b<\/li>\n<li>\u8c03\u8bd5\u548c\u4f18\u5316<\/li>\n<li>\u53d1\u5e03\u548c\u5206\u53d1<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u6211\u4eec\u53ef\u4ee5\u751f\u6210\u7b26\u5408\u8981\u6c42\u7684\u53ef\u6267\u884c\u6587\u4ef6\uff0c\u5e76\u5c06\u5176\u5206\u53d1\u7ed9\u5176\u4ed6\u7528\u6237\u3002\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u80fd\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u548c\u638c\u63e1\u5c06Python\u6587\u4ef6\u8f6c\u6362\u6210exe\u6587\u4ef6\u7684\u8fc7\u7a0b\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5c06Python\u7a0b\u5e8f\u6253\u5305\u6210\u53ef\u6267\u884c\u6587\u4ef6\uff08exe\uff09\uff1f<\/strong><br \/>\u8981\u5c06Python\u7a0b\u5e8f\u8f6c\u6362\u4e3aexe\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528\u50cfPyInstaller\u3001cx_Freeze\u6216py2exe\u7b49\u5de5\u5177\u3002PyInstaller\u662f\u6700\u5e38\u7528\u7684\u9009\u62e9\uff0c\u56e0\u4e3a\u5b83\u7b80\u5355\u6613\u7528\u5e76\u4e14\u652f\u6301\u591a\u79cd\u64cd\u4f5c\u7cfb\u7edf\u3002\u53ea\u9700\u5728\u547d\u4ee4\u884c\u4e2d\u8fd0\u884c<code>pyinstaller your_script.py<\/code>\uff0c\u7136\u540e\u5728\u751f\u6210\u7684dist\u6587\u4ef6\u5939\u4e2d\u627e\u5230exe\u6587\u4ef6\u3002<\/p>\n<p><strong>\u6253\u5305\u540e\u7684exe\u6587\u4ef6\u5728\u4e0d\u540c\u8ba1\u7b97\u673a\u4e0a\u80fd\u5426\u6b63\u5e38\u8fd0\u884c\uff1f<\/strong><br \/>\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u751f\u6210\u7684exe\u6587\u4ef6\u53ef\u4ee5\u5728\u6ca1\u6709Python\u73af\u5883\u7684\u8ba1\u7b97\u673a\u4e0a\u8fd0\u884c\uff0c\u4f46\u8981\u786e\u4fdd\u6240\u6709\u4f9d\u8d56\u7684\u5e93\u548c\u6587\u4ef6\u90fd\u5df2\u6b63\u786e\u6253\u5305\u3002\u5982\u679c\u7a0b\u5e8f\u4f9d\u8d56\u4e8e\u67d0\u4e9b\u5916\u90e8\u6587\u4ef6\u6216\u7279\u5b9a\u7684\u5e93\uff0c\u5efa\u8bae\u5728\u6253\u5305\u65f6\u4f7f\u7528PyInstaller\u7684<code>--add-data<\/code>\u53c2\u6570\uff0c\u5c06\u8fd9\u4e9b\u6587\u4ef6\u4e00\u8d77\u6253\u5305\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u6253\u5305\u540e\u51fa\u73b0\u7684\u9519\u8bef\u6216\u7f3a\u5931\u7684\u6a21\u5757\u95ee\u9898\uff1f<\/strong><br \/>\u5728\u4f7f\u7528PyInstaller\u6253\u5305\u65f6\uff0c\u5982\u679c\u9047\u5230\u9519\u8bef\u6216\u7f3a\u5931\u6a21\u5757\u7684\u60c5\u51b5\uff0c\u53ef\u4ee5\u901a\u8fc7\u67e5\u770b\u751f\u6210\u7684<code>build<\/code>\u6587\u4ef6\u5939\u4e2d\u7684\u65e5\u5fd7\u6587\u4ef6\u6765\u6392\u67e5\u95ee\u9898\u3002\u8fd8\u53ef\u4ee5\u4f7f\u7528<code>--hidden-import<\/code>\u53c2\u6570\u624b\u52a8\u6307\u5b9a\u9057\u6f0f\u7684\u6a21\u5757\uff0c\u6216\u5728.spec\u6587\u4ef6\u4e2d\u8fdb\u884c\u66f4\u8be6\u7ec6\u7684\u914d\u7f6e\u4ee5\u89e3\u51b3\u4f9d\u8d56\u95ee\u9898\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u5c06Python\u6587\u4ef6\u8f6c\u6362\u6210exe\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528\u5de5\u5177\u5982PyInstaller\u3001cx_Freeze\u548cpy2exe\u3002 [&hellip;]","protected":false},"author":3,"featured_media":1164777,"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\/1164772"}],"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=1164772"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1164772\/revisions"}],"predecessor-version":[{"id":1164779,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1164772\/revisions\/1164779"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1164777"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1164772"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1164772"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1164772"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}