{"id":1142427,"date":"2025-01-08T22:40:04","date_gmt":"2025-01-08T14:40:04","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1142427.html"},"modified":"2025-01-08T22:40:07","modified_gmt":"2025-01-08T14:40:07","slug":"%e5%a6%82%e4%bd%95%e5%b0%86python3%e9%a1%b9%e7%9b%ae%e5%81%9a%e6%88%90exe%e6%96%87%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1142427.html","title":{"rendered":"\u5982\u4f55\u5c06python3\u9879\u76ee\u505a\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\/24180552\/5ea61a5d-2c00-4e2c-9561-911dcdf70633.webp\" alt=\"\u5982\u4f55\u5c06python3\u9879\u76ee\u505a\u6210exe\u6587\u4ef6\" \/><\/p>\n<p><p> <strong>\u5c06Python3\u9879\u76ee\u8f6c\u6362\u4e3aexe\u6587\u4ef6\u7684\u5173\u952e\u6b65\u9aa4\u5305\u62ec\uff1a\u4f7f\u7528PyInstaller\u3001\u8bbe\u7f6e\u865a\u62df\u73af\u5883\u3001\u5904\u7406\u4f9d\u8d56\u9879\u3001\u914d\u7f6e.spec\u6587\u4ef6\u3001\u4f18\u5316\u548c\u6d4b\u8bd5\u3002<\/strong>\u5176\u4e2d\uff0c\u4f7f\u7528PyInstaller\u662f\u6700\u5173\u952e\u7684\u6b65\u9aa4\u3002PyInstaller\u662f\u4e00\u4e2a\u53ef\u4ee5\u5c06Python\u5e94\u7528\u7a0b\u5e8f\u6253\u5305\u6210\u72ec\u7acb\u53ef\u6267\u884c\u6587\u4ef6\u7684\u5de5\u5177\uff0c\u652f\u6301\u591a\u4e2a\u5e73\u53f0\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u6b65\u9aa4\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528PyInstaller<\/h3>\n<\/p>\n<p><p>PyInstaller\u662f\u4e00\u4e2a\u5e7f\u6cdb\u4f7f\u7528\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u5c06Python\u811a\u672c\u6253\u5305\u6210\u72ec\u7acb\u7684\u53ef\u6267\u884c\u6587\u4ef6\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u4f7f\u7528PyInstaller\u7684\u8be6\u7ec6\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b89\u88c5PyInstaller<\/strong>\uff1a\u9996\u5148\u9700\u8981\u5b89\u88c5PyInstaller\uff0c\u53ef\u4ee5\u4f7f\u7528pip\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<\/li>\n<li>\n<p><strong>\u751f\u6210\u53ef\u6267\u884c\u6587\u4ef6<\/strong>\uff1a\u5728\u547d\u4ee4\u884c\u4e2d\u5bfc\u822a\u5230\u4f60\u7684Python\u9879\u76ee\u76ee\u5f55\uff0c\u7136\u540e\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\u6765\u751f\u6210exe\u6587\u4ef6\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\u751f\u6210\u4e00\u4e2a<code>dist<\/code>\u6587\u4ef6\u5939\uff0c\u91cc\u9762\u5305\u542b\u4f60\u7684\u53ef\u6267\u884c\u6587\u4ef6\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u4e8c\u3001\u8bbe\u7f6e\u865a\u62df\u73af\u5883<\/h3>\n<\/p>\n<p><p>\u5728\u8fdb\u884c\u9879\u76ee\u6253\u5305\u4e4b\u524d\uff0c\u6700\u597d\u8bbe\u7f6e\u4e00\u4e2a\u865a\u62df\u73af\u5883\uff0c\u8fd9\u6837\u53ef\u4ee5\u907f\u514d\u4e0e\u7cfb\u7edf\u5168\u5c40\u5305\u7684\u51b2\u7a81\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u8bbe\u7f6e\u865a\u62df\u73af\u5883\u7684\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u521b\u5efa\u865a\u62df\u73af\u5883<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">python -m venv myenv<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6fc0\u6d3b\u865a\u62df\u73af\u5883<\/strong>\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u5728Windows\u4e0a\uff1a\n<pre><code class=\"language-bash\">myenv\\Scripts\\activate<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\u5728macOS\u548cLinux\u4e0a\uff1a\n<pre><code class=\"language-bash\">source myenv\/bin\/activate<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u5b89\u88c5\u9879\u76ee\u4f9d\u8d56<\/strong>\uff1a\u5728\u865a\u62df\u73af\u5883\u4e2d\u5b89\u88c5\u9879\u76ee\u6240\u9700\u7684\u6240\u6709\u4f9d\u8d56\u5305\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install -r requirements.txt<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u4e09\u3001\u5904\u7406\u4f9d\u8d56\u9879<\/h3>\n<\/p>\n<p><p>\u786e\u4fdd\u6240\u6709\u7684\u4f9d\u8d56\u9879\u90fd\u88ab\u6b63\u786e\u5b89\u88c5\uff0c\u5e76\u5728\u6253\u5305\u8fc7\u7a0b\u4e2d\u88ab\u5305\u542b\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u68c0\u67e5\u4f9d\u8d56\u9879<\/strong>\uff1a\u901a\u8fc7\u67e5\u770b<code>requirements.txt<\/code>\u6587\u4ef6\u6216\u8005\u4f7f\u7528<code>pip freeze<\/code>\u547d\u4ee4\u6765\u68c0\u67e5\u6240\u6709\u4f9d\u8d56\u9879\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip freeze &gt; requirements.txt<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u624b\u52a8\u6dfb\u52a0\u4f9d\u8d56\u9879<\/strong>\uff1a\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0cPyInstaller\u53ef\u80fd\u65e0\u6cd5\u81ea\u52a8\u68c0\u6d4b\u5230\u67d0\u4e9b\u4f9d\u8d56\u9879\uff0c\u9700\u8981\u624b\u52a8\u6307\u5b9a\u3002\u5728.spec\u6587\u4ef6\u4e2d\u6dfb\u52a0\u8fd9\u4e9b\u4f9d\u8d56\u9879\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u56db\u3001\u914d\u7f6e.spec\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>.spec\u6587\u4ef6\u662fPyInstaller\u7684\u914d\u7f6e\u6587\u4ef6\uff0c\u7528\u4e8e\u5b9a\u4e49\u6253\u5305\u8fc7\u7a0b\u4e2d\u7684\u5404\u79cd\u9009\u9879\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u914d\u7f6e.spec\u6587\u4ef6\u7684\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u751f\u6210.spec\u6587\u4ef6<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pyinstaller --name=your_project_name your_script.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u7f16\u8f91.spec\u6587\u4ef6<\/strong>\uff1a\u6253\u5f00\u751f\u6210\u7684.spec\u6587\u4ef6\uff0c\u53ef\u4ee5\u8fdb\u884c\u4e00\u4e9b\u81ea\u5b9a\u4e49\u914d\u7f6e\uff0c\u6bd4\u5982\u6dfb\u52a0\u989d\u5916\u7684\u6587\u4ef6\u3001\u6570\u636e\u8d44\u6e90\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">a = Analysis(<\/p>\n<p>    [&#39;your_script.py&#39;],<\/p>\n<p>    pathex=[&#39;path_to_your_project&#39;],<\/p>\n<p>    binaries=[],<\/p>\n<p>    datas=[(&#39;path_to_data&#39;, &#39;destination_path&#39;)],<\/p>\n<p>    ...<\/p>\n<p>)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u4e94\u3001\u4f18\u5316\u548c\u6d4b\u8bd5<\/h3>\n<\/p>\n<p><p>\u6253\u5305\u5b8c\u6210\u540e\uff0c\u9700\u8981\u8fdb\u884c\u4f18\u5316\u548c\u6d4b\u8bd5\uff0c\u4ee5\u786e\u4fdd\u751f\u6210\u7684\u53ef\u6267\u884c\u6587\u4ef6\u80fd\u591f\u6b63\u5e38\u8fd0\u884c\u4e14\u6027\u80fd\u826f\u597d\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u6d4b\u8bd5\u53ef\u6267\u884c\u6587\u4ef6<\/strong>\uff1a\u5728\u4e0d\u540c\u7684\u7cfb\u7edf\u4e0a\u8fd0\u884c\u751f\u6210\u7684exe\u6587\u4ef6\uff0c\u786e\u4fdd\u5b83\u80fd\u591f\u6b63\u5e38\u5de5\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">.\/dist\/your_project_name\/your_project_name.exe<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f18\u5316\u6587\u4ef6\u5927\u5c0f<\/strong>\uff1a\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e9b\u5de5\u5177\u6765\u51cf\u5c11\u53ef\u6267\u884c\u6587\u4ef6\u7684\u5927\u5c0f\uff0c\u6bd4\u5982UPX\uff08Ultimate Packer for eXecutables\uff09\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install upx<\/p>\n<p>upx --best --lzma your_executable.exe<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5904\u7406\u5e38\u89c1\u95ee\u9898<\/strong>\uff1a\u6709\u65f6\u6253\u5305\u8fc7\u7a0b\u4e2d\u4f1a\u9047\u5230\u4e00\u4e9b\u95ee\u9898\uff0c\u6bd4\u5982\u7f3a\u5c11DLL\u6587\u4ef6\u3001\u8def\u5f84\u95ee\u9898\u7b49\u3002\u9700\u8981\u6839\u636e\u5177\u4f53\u7684\u9519\u8bef\u4fe1\u606f\u8fdb\u884c\u8c03\u8bd5\u548c\u4fee\u590d\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u516d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5c06Python3\u9879\u76ee\u6253\u5305\u6210exe\u6587\u4ef6\u9700\u8981\u6ce8\u610f\u4ee5\u4e0b\u51e0\u70b9\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u9009\u62e9\u5408\u9002\u7684\u6253\u5305\u5de5\u5177<\/strong>\uff1a\u867d\u7136PyInstaller\u662f\u6700\u5e38\u7528\u7684\u5de5\u5177\uff0c\u4f46\u4e5f\u6709\u5176\u4ed6\u5de5\u5177\u5982cx_Freeze\u3001py2exe\u7b49\uff0c\u53ef\u4ee5\u6839\u636e\u9879\u76ee\u9700\u6c42\u9009\u62e9\u3002<\/li>\n<li><strong>\u8bbe\u7f6e\u865a\u62df\u73af\u5883<\/strong>\uff1a\u786e\u4fdd\u9879\u76ee\u4f9d\u8d56\u7684\u72ec\u7acb\u6027\uff0c\u907f\u514d\u4e0e\u7cfb\u7edf\u5168\u5c40\u5305\u7684\u51b2\u7a81\u3002<\/li>\n<li><strong>\u5904\u7406\u4f9d\u8d56\u9879<\/strong>\uff1a\u786e\u4fdd\u6240\u6709\u7684\u4f9d\u8d56\u9879\u90fd\u88ab\u6b63\u786e\u5b89\u88c5\u548c\u6253\u5305\u3002<\/li>\n<li><strong>\u914d\u7f6e.spec\u6587\u4ef6<\/strong>\uff1a\u6839\u636e\u9879\u76ee\u9700\u6c42\u8fdb\u884c\u81ea\u5b9a\u4e49\u914d\u7f6e\uff0c\u6dfb\u52a0\u989d\u5916\u7684\u6587\u4ef6\u548c\u8d44\u6e90\u3002<\/li>\n<li><strong>\u4f18\u5316\u548c\u6d4b\u8bd5<\/strong>\uff1a\u751f\u6210\u7684\u53ef\u6267\u884c\u6587\u4ef6\u9700\u8981\u8fdb\u884c\u5145\u5206\u7684\u6d4b\u8bd5\u548c\u4f18\u5316\uff0c\u786e\u4fdd\u6027\u80fd\u548c\u7a33\u5b9a\u6027\u3002<\/li>\n<\/ul>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u4f60\u53ef\u4ee5\u5c06Python3\u9879\u76ee\u6210\u529f\u6253\u5305\u6210exe\u6587\u4ef6\uff0c\u5e76\u4e14\u786e\u4fdd\u5176\u5728\u76ee\u6807\u7cfb\u7edf\u4e0a\u6b63\u5e38\u8fd0\u884c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5c06Python3\u9879\u76ee\u8f6c\u6362\u4e3a\u53ef\u6267\u884c\u6587\u4ef6\uff08exe\uff09\uff1f<\/strong><br \/>\u5c06Python3\u9879\u76ee\u6253\u5305\u4e3aexe\u6587\u4ef6\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e9b\u6d41\u884c\u7684\u5de5\u5177\uff0c\u5982PyInstaller\u6216cx_Freeze\u3002\u8fd9\u4e9b\u5de5\u5177\u80fd\u591f\u5c06Python\u4ee3\u7801\u53ca\u5176\u4f9d\u8d56\u9879\u6253\u5305\u6210\u4e00\u4e2a\u72ec\u7acb\u7684\u53ef\u6267\u884c\u6587\u4ef6\uff0c\u7528\u6237\u4e0d\u9700\u8981\u5b89\u88c5Python\u73af\u5883\u5373\u53ef\u8fd0\u884c\u3002\u901a\u5e38\uff0c\u7528\u6237\u53ea\u9700\u5728\u547d\u4ee4\u884c\u4e2d\u8f93\u5165\u76f8\u5e94\u7684\u547d\u4ee4\uff0c\u5de5\u5177\u5c31\u4f1a\u81ea\u52a8\u5904\u7406\u6253\u5305\u8fc7\u7a0b\u3002\u9009\u62e9\u5408\u9002\u7684\u5de5\u5177\u548c\u914d\u7f6e\u6253\u5305\u9009\u9879\u662f\u786e\u4fddexe\u6587\u4ef6\u6b63\u5e38\u8fd0\u884c\u7684\u5173\u952e\u3002<\/p>\n<p><strong>\u5728\u5c06Python\u9879\u76ee\u6253\u5305\u4e3aexe\u6587\u4ef6\u4e4b\u524d\u9700\u8981\u6ce8\u610f\u54ea\u4e9b\u4e8b\u9879\uff1f<\/strong><br \/>\u5728\u6253\u5305\u4e4b\u524d\uff0c\u786e\u4fdd\u9879\u76ee\u4e2d\u7684\u6240\u6709\u4f9d\u8d56\u5e93\u5df2\u5b89\u88c5\u5e76\u4e14\u4ee3\u7801\u6ca1\u6709\u9519\u8bef\u3002\u8003\u8651\u5230\u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\u7684\u517c\u5bb9\u6027\uff0c\u5efa\u8bae\u5728\u76ee\u6807\u64cd\u4f5c\u7cfb\u7edf\u4e0b\u8fdb\u884c\u6253\u5305\u3002\u6b64\u5916\uff0c\u68c0\u67e5\u7a0b\u5e8f\u7684\u8f93\u5165\u8f93\u51fa\u8def\u5f84\uff0c\u786e\u4fdd\u5728\u6253\u5305\u65f6\u4e0d\u4f1a\u5f15\u5165\u8def\u5f84\u95ee\u9898\u3002\u6839\u636e\u9879\u76ee\u7684\u590d\u6742\u6027\uff0c\u53ef\u80fd\u9700\u8981\u914d\u7f6espec\u6587\u4ef6\uff0c\u4ee5\u4fbf\u6b63\u786e\u5904\u7406\u8d44\u6e90\u6587\u4ef6\u548c\u6570\u636e\u6587\u4ef6\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u6253\u5305\u540e\u7684exe\u6587\u4ef6\u4e2d\u7684\u56fe\u6807\u548c\u8d44\u6e90\u6587\u4ef6\uff1f<\/strong><br \/>\u5bf9\u4e8e\u5e0c\u671b\u5728exe\u6587\u4ef6\u4e2d\u6dfb\u52a0\u81ea\u5b9a\u4e49\u56fe\u6807\u6216\u8d44\u6e90\u6587\u4ef6\u7684\u7528\u6237\uff0c\u53ef\u4ee5\u5728\u4f7f\u7528PyInstaller\u6216\u5176\u4ed6\u5de5\u5177\u65f6\uff0c\u901a\u8fc7\u6307\u5b9a\u76f8\u5e94\u7684\u53c2\u6570\u6765\u5b9e\u73b0\u3002\u4f8b\u5982\uff0c\u4f7f\u7528PyInstaller\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7<code>--icon<\/code>\u53c2\u6570\u6765\u8bbe\u7f6e\u56fe\u6807\uff0c\u4f7f\u7528<code>--add-data<\/code>\u53c2\u6570\u6765\u5305\u542b\u989d\u5916\u7684\u8d44\u6e90\u6587\u4ef6\u3002\u5728\u6253\u5305\u65f6\uff0c\u786e\u4fdd\u8d44\u6e90\u6587\u4ef6\u7684\u8def\u5f84\u6b63\u786e\uff0c\u4ee5\u4fbf\u5728\u8fd0\u884c\u65f6\u80fd\u591f\u627e\u5230\u8fd9\u4e9b\u6587\u4ef6\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5c06Python3\u9879\u76ee\u8f6c\u6362\u4e3aexe\u6587\u4ef6\u7684\u5173\u952e\u6b65\u9aa4\u5305\u62ec\uff1a\u4f7f\u7528PyInstaller\u3001\u8bbe\u7f6e\u865a\u62df\u73af\u5883\u3001\u5904\u7406\u4f9d\u8d56\u9879\u3001\u914d\u7f6e [&hellip;]","protected":false},"author":3,"featured_media":1142436,"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\/1142427"}],"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=1142427"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1142427\/revisions"}],"predecessor-version":[{"id":1142441,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1142427\/revisions\/1142441"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1142436"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1142427"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1142427"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1142427"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}