{"id":261975,"date":"2024-05-15T14:19:39","date_gmt":"2024-05-15T06:19:39","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/261975.html"},"modified":"2024-05-15T14:19:44","modified_gmt":"2024-05-15T06:19:44","slug":"%e5%a6%82%e4%bd%95%e9%80%9a%e8%bf%87-python3-7-%e5%b0%86%e4%bb%a3%e7%a0%81%e6%89%93%e5%8c%85%e6%88%90-exe-%e7%a8%8b%e5%ba%8f%e5%b9%b6%e6%b7%bb%e5%8a%a0%e5%9b%be%e6%a0%87","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/261975.html","title":{"rendered":"\u5982\u4f55\u901a\u8fc7  python3.7 \u5c06\u4ee3\u7801\u6253\u5305\u6210 exe \u7a0b\u5e8f\u5e76\u6dfb\u52a0\u56fe\u6807"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27121604\/0f1eb418-f090-4ea8-b349-c30ec7ef8daa.webp\" alt=\"\u5982\u4f55\u901a\u8fc7  python3.7 \u5c06\u4ee3\u7801\u6253\u5305\u6210 exe \u7a0b\u5e8f\u5e76\u6dfb\u52a0\u56fe\u6807\" \/><\/p>\n<p><p>\u901a\u8fc7Python 3.7\u5c06\u4ee3\u7801\u6253\u5305\u6210exe\u7a0b\u5e8f\u5e76\u6dfb\u52a0\u56fe\u6807\u7684\u65b9\u6cd5\u4e3b\u8981\u6d89\u53ca<strong>\u4f7f\u7528pyinstaller\u5e93, \u6253\u5305\u65f6\u6307\u5b9a\u56fe\u6807\u6587\u4ef6\u3001\u7f16\u5199\u7b26\u5408\u89c4\u8303\u7684spec\u6587\u4ef6<\/strong>\u3002\u60a8\u53ef\u4ee5\u5728\u5f00\u53d1\u73af\u5883\u4e2d\u901a\u8fc7pip\u5b89\u88c5pyinstaller\uff0c\u7136\u540e\u5229\u7528\u5176\u547d\u4ee4\u884c\u5de5\u5177\u5c06\u60a8\u7684Python\u811a\u672c\u8f6c\u6362\u4e3a\u5355\u4e2a\u53ef\u6267\u884c\u6587\u4ef6\u3002\u5728\u6253\u5305\u8fc7\u7a0b\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7<code>--icon<\/code>\u53c2\u6570\u6307\u5b9a\u81ea\u5b9a\u4e49\u56fe\u6807\u6587\u4ef6\u3002\u6269\u5c55\u89e3\u91ca\u4e00\u70b9\uff1a\u5728\u6307\u5b9a\u56fe\u6807\u6587\u4ef6\u65f6\uff0c\u60a8\u9700\u8981\u786e\u4fdd\u56fe\u6807\u662f<code>.ico<\/code>\u683c\u5f0f\uff0c\u8fd9\u662fWindows\u7cfb\u7edf\u8bc6\u522b\u7684\u56fe\u6807\u683c\u5f0f\u3002\u53ef\u4ee5\u4f7f\u7528\u5728\u7ebf\u5de5\u5177\u6216\u56fe\u5f62\u7f16\u8f91\u8f6f\u4ef6\u5c06PNG\u6216\u5176\u4ed6\u56fe\u7247\u683c\u5f0f\u8f6c\u6362\u4e3aICO\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5b89\u88c5\u4e0e\u51c6\u5907\u5de5\u4f5c<\/h3>\n<\/p>\n<p><p>\u5728\u6253\u5305Python\u4ee3\u7801\u4e4b\u524d\uff0c\u9996\u5148\u9700\u8981\u786e\u4fdd\u7cfb\u7edf\u4e2d\u5b89\u88c5\u4e86Python 3.7\u548cpip\u3002\u6253\u5305\u5de5\u5177PyInstaller\u53ef\u4ee5\u901a\u8fc7pip\u8f7b\u677e\u5b89\u88c5\u3002\u6253\u5f00\u547d\u4ee4\u884c\u5de5\u5177\uff0c\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\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><p><strong>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u68c0\u67e5\u662f\u5426\u5b89\u88c5\u6210\u529f<\/strong>\uff0c\u53ef\u4ee5\u4f7f\u7528<code>pyinstaller --version<\/code>\u6765\u68c0\u9a8cPyInstaller\u662f\u5426\u5df2\u6b63\u786e\u5b89\u88c5\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u521b\u5efa\u5e76\u6d4b\u8bd5Python\u811a\u672c<\/h3>\n<\/p>\n<p><p>\u5728\u6253\u5305\u524d\uff0c\u786e\u4fdd\u60a8\u7684Python\u811a\u672c\u80fd\u591f\u5728Python 3.7\u73af\u5883\u4e2d\u6b63\u786e\u8fd0\u884c\u3002<\/p>\n<\/p>\n<p><p><strong>\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684Python\u5e94\u7528<\/strong>\uff0c\u4f5c\u4e3a\u6253\u5305\u7684\u4f8b\u5b50\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u7f16\u5199\u4e00\u4e2a\u7b80\u5355\u7684GUI\u7a0b\u5e8f\uff0c\u5e76\u786e\u4fdd\u5b83\u65e0\u9519\u8bef\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import tkinter as tk<\/p>\n<p>def m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n():<\/p>\n<p>    root = tk.Tk()<\/p>\n<p>    root.title(&#039;Example App&#039;)<\/p>\n<p>    label = tk.Label(root, text=&#039;Hello, World!&#039;)<\/p>\n<p>    label.pack()<\/p>\n<p>    root.mainloop()<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528PyInstaller\u6253\u5305\u811a\u672c<\/h3>\n<\/p>\n<p><p>\u5728\u786e\u4fdd\u811a\u672c\u53ef\u4ee5\u6b63\u5e38\u8fd0\u884c\u540e\uff0c<strong>\u4f7f\u7528PyInstaller\u8fdb\u884c\u6253\u5305<\/strong>\u3002\u6253\u5f00\u547d\u4ee4\u884c\u5de5\u5177\uff0c\u5207\u6362\u5230\u811a\u672c\u6240\u5728\u76ee\u5f55\uff0c\u6267\u884c\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pyinstaller --onefile --windowed --icon=app_icon.ico your_script.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u91cc\uff1a<\/p>\n<\/p>\n<ul>\n<li><code>--onefile<\/code>\uff1a\u521b\u5efa\u5355\u4e2a\u53ef\u6267\u884c\u6587\u4ef6\u3002<\/li>\n<li><code>--windowed<\/code>\uff1a\u6307\u793a\u6253\u5305\u7684\u5e94\u7528\u4e3a\u7a97\u53e3\u5e94\u7528\uff0c\u4e0d\u663e\u793a\u547d\u4ee4\u884c\u7a97\u53e3\uff08\u9002\u7528\u4e8eGUI\u5e94\u7528\uff09\u3002<\/li>\n<li><code>--icon<\/code>\uff1a\u540e\u9762\u8ddf\u4e0a\u4f60\u7684\u56fe\u6807\u8def\u5f84\uff0c\u56fe\u6807\u9700\u8981\u662f.ico\u683c\u5f0f\u3002<\/li>\n<\/ul>\n<p><h3>\u56db\u3001\u81ea\u5b9a\u4e49\u6253\u5305\u8fc7\u7a0b<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u547d\u4ee4\u884c\u53c2\u6570\u4e4b\u5916\uff0cPyInstaller\u5141\u8bb8\u4f7f\u7528spec\u6587\u4ef6\u6765\u66f4\u7cbe\u7ec6\u5730\u63a7\u5236\u6253\u5305\u8fc7\u7a0b\u3002<\/p>\n<\/p>\n<p><p><strong>\u9996\u5148\u751f\u6210\u521d\u59cb\u7684spec\u6587\u4ef6<\/strong>\u3002\u4f7f\u7528\u5982\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\u751f\u6210<code>your_script.spec<\/code>\u6587\u4ef6\u3002\u4f7f\u7528\u6587\u672c\u7f16\u8f91\u5668\u6253\u5f00\u8fd9\u4e2a\u6587\u4ef6\uff0c\u53ef\u4ee5\u770b\u5230\u5f88\u591a\u914d\u7f6e\u9009\u9879\u3002<\/p>\n<\/p>\n<p><p><strong>\u8fdb\u884c\u5fc5\u8981\u7684\u4fee\u6539<\/strong>\u6765\u81ea\u5b9a\u4e49\u60a8\u7684\u6253\u5305\u8fc7\u7a0b\uff0c\u4f8b\u5982\u6dfb\u52a0\u6570\u636e\u6587\u4ef6\u3001\u66f4\u6539\u6253\u5305\u76ee\u5f55\u7b49\u3002\u5728spec\u6587\u4ef6\u4e2d\u8fd8\u53ef\u4ee5\u6307\u5b9a\u56fe\u6807\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">a = Analysis([&#039;your_script.py&#039;],<\/p>\n<p>             ...<\/p>\n<p>             icon=&#039;app_icon.ico&#039;,<\/p>\n<p>             ...)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4fee\u6539\u5b8c\u6bd5\u540e\uff0c\u4f7f\u7528PyInstaller\u518d\u6b21\u6253\u5305\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pyinstaller your_script.spec<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u6d4b\u8bd5\u6253\u5305\u7a0b\u5e8f<\/h3>\n<\/p>\n<p><p><strong>\u6253\u5305\u540e\u7684\u53ef\u6267\u884c\u6587\u4ef6\u5c06\u4f4d\u4e8edist\u76ee\u5f55<\/strong>\u3002\u5728Windows\u73af\u5883\u4e0b\uff0c\u76f4\u63a5\u53cc\u51fb\u53ef\u6267\u884c\u6587\u4ef6\uff0c\u6d4b\u8bd5\u7a0b\u5e8f\u662f\u5426\u6309\u9884\u671f\u8fd0\u884c\u3002\u5982\u679c\u7a0b\u5e8f\u6d89\u53ca\u5230\u6587\u4ef6\u8bfb\u5199\u6216\u7f51\u7edc\u901a\u4fe1\u7b49\u64cd\u4f5c\uff0c\u786e\u4fdd\u6d4b\u8bd5\u8fd9\u4e9b\u529f\u80fd\u90fd\u80fd\u6b63\u5e38\u5de5\u4f5c\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u89e3\u51b3\u6253\u5305\u4e2d\u9047\u5230\u7684\u95ee\u9898<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\uff0c\u6253\u5305\u540e\u7684\u7a0b\u5e8f\u53ef\u80fd\u4f1a\u51fa\u73b0\u4e00\u4e9b\u95ee\u9898\uff0c\u4f8b\u5982\u7f3a\u5c11\u5e93\u6587\u4ef6\u6216\u8def\u5f84\u9519\u8bef\u7b49\u3002<\/p>\n<\/p>\n<p><p><strong>\u68c0\u67e5PyInstaller\u7684\u547d\u4ee4\u884c\u8f93\u51fa<\/strong>\uff0c\u7279\u522b\u7559\u610fWARNING\u4fe1\u606f\uff0c\u5bfb\u627e\u53ef\u80fd\u7684\u9519\u8bef\u539f\u56e0\u3002<\/p>\n<\/p>\n<p><p>\u5982\u679c\u7a0b\u5e8f\u4f9d\u8d56\u4e8e\u5176\u4ed6\u6570\u636e\u6587\u4ef6\u6216\u8005\u7b2c\u4e09\u65b9\u5e93\uff0c\u786e\u4fdd\u5728spec\u6587\u4ef6\u4e2d\u6b63\u786e\u6dfb\u52a0\u4e86\u8fd9\u4e9b\u8d44\u6e90\u3002\u5982\u6709\u5fc5\u8981\uff0c\u624b\u52a8\u6307\u5b9a\u7a0b\u5e8f\u8fd0\u884c\u65f6\u7684\u8def\u5f84\u7b49\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u4f18\u5316\u6253\u5305\u7ed3\u679c<\/h3>\n<\/p>\n<p><p>\u9488\u5bf9\u6700\u7ec8\u53d1\u5e03\u7684\u7248\u672c\uff0c\u53ef\u80fd\u9700\u8981<strong>\u4f18\u5316\u6253\u5305\u7ed3\u679c\uff0c\u4f8b\u5982\u51cf\u5c0f\u6587\u4ef6\u4f53\u79ef\u3001\u9690\u85cf\u63a7\u5236\u53f0\u7a97\u53e3<\/strong>\u7b49\u3002<\/p>\n<\/p>\n<p><p>\u4f7f\u7528UPX\uff08Ultimate Packer for Executables\uff09\u53ef\u4ee5\u51cf\u5c0f\u751f\u6210\u7684exe\u6587\u4ef6\u7684\u4f53\u79ef\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pyinstaller --onefile --windowed --icon=app_icon.ico --upx-dir=\/path\/to\/upx your_script.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u786e\u4fdd\u60a8\u4e0b\u8f7d\u5e76\u89e3\u538b\u4e86UPX\uff0c\u5e76\u4e14\u5728<code>--upx-dir<\/code>\u53c2\u6570\u6307\u5b9a\u4e86UPX\u7684\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><p>\u5728\u6574\u4e2a\u6253\u5305\u8fc7\u7a0b\u4e2d\uff0c\u9700\u8981\u5bc6\u5207\u5173\u6ce8\u6253\u5305\u65e5\u5fd7\uff0c\u53ca\u65f6\u53d1\u73b0\u5e76\u89e3\u51b3\u95ee\u9898\u3002\u7ecf\u8fc7\u4e0a\u8ff0\u6b65\u9aa4\uff0c\u60a8\u5c06\u80fd\u591f\u5f97\u5230\u4e00\u4e2a\u5e26\u6709\u81ea\u5b9a\u4e49\u56fe\u6807\u7684\u3001\u72ec\u7acb\u7684exe\u6587\u4ef6\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>1. \u5982\u4f55\u4f7f\u7528Python3.7\u5c06\u4ee3\u7801\u6253\u5305\u6210\u53ef\u6267\u884c\u7684exe\u7a0b\u5e8f\uff1f<\/strong><\/p>\n<p>\u8981\u5c06Python\u4ee3\u7801\u6253\u5305\u6210\u53ef\u6267\u884c\u7684exe\u7a0b\u5e8f\uff0c\u53ef\u4ee5\u4f7f\u7528PyInstaller\u8fd9\u4e2a\u5e38\u7528\u7684\u5de5\u5177\u3002PyInstaller\u53ef\u4ee5\u5c06Python\u4ee3\u7801\u53ca\u5176\u6240\u6709\u4f9d\u8d56\u9879\u6253\u5305\u6210\u5355\u4e2a\u53ef\u6267\u884c\u6587\u4ef6\u3002\u8981\u4f7f\u7528PyInstaller\uff0c\u9996\u5148\u786e\u4fdd\u5df2\u7ecf\u5b89\u88c5\u4e86Python3.7\u548cPyInstaller\u3002\u7136\u540e\uff0c\u6253\u5f00\u547d\u4ee4\u884c\u7ec8\u7aef\u5e76\u5207\u6362\u5230\u5305\u542b\u4f60\u7684Python\u4ee3\u7801\u7684\u76ee\u5f55\u3002\u63a5\u4e0b\u6765\uff0c\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<pre><code>pyinstaller your_script.py\n<\/code><\/pre>\n<p>\u8fd9\u5c06\u751f\u6210\u4e00\u4e2adist\u6587\u4ef6\u5939\uff0c\u5176\u4e2d\u5305\u542b\u53ef\u6267\u884c\u7684exe\u6587\u4ef6\u548c\u5176\u4ed6\u6240\u9700\u7684\u6587\u4ef6\u3002\u4f60\u53ef\u4ee5\u5728dist\u6587\u4ef6\u5939\u4e2d\u627e\u5230\u751f\u6210\u7684exe\u7a0b\u5e8f\uff0c\u5e76\u5c06\u5176\u8fd0\u884c\u5728\u4efb\u4f55\u652f\u6301\u7684\u64cd\u4f5c\u7cfb\u7edf\u4e0a\u3002<\/p>\n<p><strong>2. \u5982\u4f55\u4e3a\u6253\u5305\u540e\u7684exe\u7a0b\u5e8f\u6dfb\u52a0\u56fe\u6807\uff1f<\/strong><\/p>\n<p>\u8981\u4e3a\u6253\u5305\u540e\u7684exe\u7a0b\u5e8f\u6dfb\u52a0\u56fe\u6807\uff0c\u53ef\u4ee5\u4f7f\u7528PyInstaller\u63d0\u4f9b\u7684\u4e00\u4e9b\u53c2\u6570\u6765\u5b9e\u73b0\u3002\u9996\u5148\uff0c\u786e\u4fdd\u56fe\u6807\u6587\u4ef6\u4ee5.ico\u683c\u5f0f\u5b58\u5728\u3002\u7136\u540e\uff0c\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<pre><code>pyinstaller --icon=your_icon.ico your_script.py\n<\/code><\/pre>\n<p>\u8fd9\u5c06\u751f\u6210\u4e00\u4e2a\u5e26\u6709\u6307\u5b9a\u56fe\u6807\u7684exe\u7a0b\u5e8f\u3002\u786e\u4fdd\u5728\u8fd0\u884cPyInstaller\u547d\u4ee4\u65f6\u63d0\u4f9b\u6b63\u786e\u7684\u56fe\u6807\u6587\u4ef6\u8def\u5f84\u548c\u4ee3\u7801\u6587\u4ef6\u540d\u3002<\/p>\n<p><strong>3. \u5982\u4f55\u5c06\u6253\u5305\u540e\u7684exe\u7a0b\u5e8f\u4e0e\u56fe\u6807\u6587\u4ef6\u653e\u5728\u540c\u4e00\u6587\u4ef6\u5939\u4e2d\uff1f<\/strong><\/p>\n<p>\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0cPyInstaller\u5c06\u751f\u6210\u7684\u53ef\u6267\u884c\u6587\u4ef6\u548c\u6240\u9700\u7684\u6587\u4ef6\u653e\u5728dist\u6587\u4ef6\u5939\u4e2d\u3002\u5982\u679c\u8981\u5c06\u6253\u5305\u540e\u7684exe\u7a0b\u5e8f\u4e0e\u56fe\u6807\u6587\u4ef6\u653e\u5728\u540c\u4e00\u6587\u4ef6\u5939\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>--onedir<\/code>\u53c2\u6570\u3002\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<pre><code>pyinstaller --icon=your_icon.ico --onedir your_script.py\n<\/code><\/pre>\n<p>\u8fd9\u5c06\u751f\u6210\u4e00\u4e2a\u5305\u542bexe\u7a0b\u5e8f\u548c\u56fe\u6807\u6587\u4ef6\u7684\u6587\u4ef6\u5939\uff0c\u800c\u4e0d\u662f\u5355\u4e2aexe\u6587\u4ef6\u3002\u786e\u4fdd\u5728\u8fd0\u884cPyInstaller\u547d\u4ee4\u65f6\u63d0\u4f9b\u6b63\u786e\u7684\u56fe\u6807\u6587\u4ef6\u8def\u5f84\u548c\u4ee3\u7801\u6587\u4ef6\u540d\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u901a\u8fc7Python 3.7\u5c06\u4ee3\u7801\u6253\u5305\u6210exe\u7a0b\u5e8f\u5e76\u6dfb\u52a0\u56fe\u6807\u7684\u65b9\u6cd5\u4e3b\u8981\u6d89\u53ca\u4f7f\u7528pyinstaller\u5e93, \u6253\u5305\u65f6\u6307 [&hellip;]","protected":false},"author":3,"featured_media":261978,"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\/261975"}],"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=261975"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/261975\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/261978"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=261975"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=261975"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=261975"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}