{"id":1159807,"date":"2025-01-13T18:57:17","date_gmt":"2025-01-13T10:57:17","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1159807.html"},"modified":"2025-01-13T18:57:19","modified_gmt":"2025-01-13T10:57:19","slug":"python%e7%9a%84%e8%84%9a%e6%9c%ac%e5%a6%82%e4%bd%95%e5%8f%98%e5%9b%be%e6%a0%87","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1159807.html","title":{"rendered":"python\u7684\u811a\u672c\u5982\u4f55\u53d8\u56fe\u6807"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25201438\/37c597c0-19f4-4366-870e-7dde23cf56e3.webp\" alt=\"python\u7684\u811a\u672c\u5982\u4f55\u53d8\u56fe\u6807\" \/><\/p>\n<p><p> <strong>Python\u811a\u672c\u53d8\u56fe\u6807\u7684\u65b9\u6cd5\u6709\u4ee5\u4e0b\u51e0\u79cd\uff1a\u4f7f\u7528pyinstaller\u5de5\u5177\u3001\u4f7f\u7528cx_Freeze\u5de5\u5177\u3001\u4f7f\u7528py2exe\u5de5\u5177\u3002<\/strong> <\/p>\n<\/p>\n<p><p>\u5176\u4e2d\uff0c\u4f7f\u7528pyinstaller\u5de5\u5177\u662f\u6700\u5e38\u89c1\u4e14\u7b80\u5355\u7684\u65b9\u6cd5\u3002PyInstaller\u662f\u4e00\u4e2a\u5c06Python\u5e94\u7528\u6253\u5305\u6210\u72ec\u7acb\u7684\u53ef\u6267\u884c\u6587\u4ef6\u7684\u5de5\u5177\u3002\u5b83\u652f\u6301\u5c06\u5e94\u7528\u7a0b\u5e8f\u53ca\u5176\u6240\u6709\u4f9d\u8d56\u9879\u6253\u5305\u6210\u4e00\u4e2a\u53ef\u6267\u884c\u6587\u4ef6\uff0c\u65b9\u4fbf\u5206\u53d1\u3002\u4ee5\u4e0b\u662f\u8be6\u7ec6\u63cf\u8ff0\u5982\u4f55\u4f7f\u7528pyinstaller\u5de5\u5177\u6765\u4e3aPython\u811a\u672c\u53d8\u56fe\u6807\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528 PyInstaller \u5de5\u5177<\/h3>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b89\u88c5 PyInstaller<\/strong><\/p>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u4f60\u9700\u8981\u5b89\u88c5 PyInstaller\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528 pip \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<\/li>\n<li>\n<p><strong>\u51c6\u5907\u56fe\u6807\u6587\u4ef6<\/strong><\/p>\n<\/p>\n<p><p>\u51c6\u5907\u4e00\u4e2a .ico \u683c\u5f0f\u7684\u56fe\u6807\u6587\u4ef6\uff0c\u786e\u4fdd\u6587\u4ef6\u5b58\u653e\u5728\u4e0e\u4f60\u7684 Python \u811a\u672c\u76f8\u540c\u7684\u76ee\u5f55\u4e2d\uff0c\u6216\u8005\u4f60\u77e5\u9053\u5b83\u7684\u7edd\u5bf9\u8def\u5f84\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6253\u5305 Python \u811a\u672c<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528 PyInstaller \u7684 <code>--icon<\/code> \u53c2\u6570\u6765\u6307\u5b9a\u56fe\u6807\u6587\u4ef6\u3002\u4f8b\u5982\uff0c\u5047\u8bbe\u4f60\u7684\u811a\u672c\u540d\u4e3a <code>myscript.py<\/code>\uff0c\u56fe\u6807\u6587\u4ef6\u540d\u4e3a <code>myicon.ico<\/code>\uff0c\u4f60\u53ef\u4ee5\u5728\u547d\u4ee4\u884c\u4e2d\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pyinstaller --onefile --icon=myicon.ico myscript.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u547d\u4ee4\u4f1a\u751f\u6210\u4e00\u4e2a\u72ec\u7acb\u7684\u53ef\u6267\u884c\u6587\u4ef6\uff0c\u5e76\u5c06\u56fe\u6807\u5d4c\u5165\u5230\u751f\u6210\u7684\u53ef\u6267\u884c\u6587\u4ef6\u4e2d\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u68c0\u67e5\u8f93\u51fa\u6587\u4ef6<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u540c\u4e00\u76ee\u5f55\u4e0b\uff0cPyInstaller \u4f1a\u751f\u6210\u4e00\u4e2a\u540d\u4e3a <code>dist<\/code> \u7684\u6587\u4ef6\u5939\uff0c\u91cc\u9762\u5305\u542b\u4f60\u7684\u53ef\u6267\u884c\u6587\u4ef6\u3002\u8fd0\u884c\u8fd9\u4e2a\u53ef\u6267\u884c\u6587\u4ef6\uff0c\u4f60\u4f1a\u53d1\u73b0\u5b83\u5df2\u7ecf\u6709\u4e86\u4f60\u6307\u5b9a\u7684\u56fe\u6807\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u4e8c\u3001\u4f7f\u7528 cx_Freeze \u5de5\u5177<\/h3>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b89\u88c5 cx_Freeze<\/strong><\/p>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u4f7f\u7528 pip \u6765\u5b89\u88c5 cx_Freeze\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install cx_Freeze<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u51c6\u5907\u56fe\u6807\u6587\u4ef6<\/strong><\/p>\n<\/p>\n<p><p>\u540c\u6837\uff0c\u51c6\u5907\u4e00\u4e2a .ico \u683c\u5f0f\u7684\u56fe\u6807\u6587\u4ef6\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u521b\u5efa setup.py \u6587\u4ef6<\/strong><\/p>\n<\/p>\n<p><p>\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a <code>setup.py<\/code> \u7684\u6587\u4ef6\uff0c\u5e76\u6dfb\u52a0\u4ee5\u4e0b\u5185\u5bb9\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from cx_Freeze import setup, Executable<\/p>\n<h2><strong>Replace &#39;myscript.py&#39; with your script&#39;s name<\/strong><\/h2>\n<h2><strong>Replace &#39;myicon.ico&#39; with your icon file&#39;s name<\/strong><\/h2>\n<p>setup(<\/p>\n<p>    name = &quot;MyScript&quot;,<\/p>\n<p>    version = &quot;0.1&quot;,<\/p>\n<p>    description = &quot;My script description&quot;,<\/p>\n<p>    executables = [Executable(&quot;myscript.py&quot;, icon=&quot;myicon.ico&quot;)]<\/p>\n<p>)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8fd0\u884c setup.py<\/strong><\/p>\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\u4e2a\u547d\u4ee4\u4f1a\u5728 <code>build<\/code> \u76ee\u5f55\u4e2d\u751f\u6210\u4e00\u4e2a\u5305\u542b\u4f60\u7684\u811a\u672c\u7684\u53ef\u6267\u884c\u6587\u4ef6\uff0c\u5e76\u9644\u5e26\u6307\u5b9a\u7684\u56fe\u6807\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u4e09\u3001\u4f7f\u7528 py2exe \u5de5\u5177<\/h3>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b89\u88c5 py2exe<\/strong><\/p>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u4f7f\u7528 pip \u6765\u5b89\u88c5 py2exe\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install py2exe<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u51c6\u5907\u56fe\u6807\u6587\u4ef6<\/strong><\/p>\n<\/p>\n<p><p>\u51c6\u5907\u4e00\u4e2a .ico \u683c\u5f0f\u7684\u56fe\u6807\u6587\u4ef6\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u521b\u5efa setup.py \u6587\u4ef6<\/strong><\/p>\n<\/p>\n<p><p>\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a <code>setup.py<\/code> \u7684\u6587\u4ef6\uff0c\u5e76\u6dfb\u52a0\u4ee5\u4e0b\u5185\u5bb9\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from distutils.core import setup<\/p>\n<p>import py2exe<\/p>\n<h2><strong>Replace &#39;myscript.py&#39; with your script&#39;s name<\/strong><\/h2>\n<h2><strong>Replace &#39;myicon.ico&#39; with your icon file&#39;s name<\/strong><\/h2>\n<p>setup(<\/p>\n<p>    windows=[{<\/p>\n<p>        &quot;script&quot;: &quot;myscript.py&quot;,<\/p>\n<p>        &quot;icon_resources&quot;: [(0, &quot;myicon.ico&quot;)]<\/p>\n<p>    }],<\/p>\n<p>)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8fd0\u884c setup.py<\/strong><\/p>\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 py2exe<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u547d\u4ee4\u4f1a\u5728 <code>dist<\/code> \u76ee\u5f55\u4e2d\u751f\u6210\u4e00\u4e2a\u5305\u542b\u4f60\u7684\u811a\u672c\u7684\u53ef\u6267\u884c\u6587\u4ef6\uff0c\u5e76\u9644\u5e26\u6307\u5b9a\u7684\u56fe\u6807\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u7ec6\u8282\u4e0e\u6ce8\u610f\u4e8b\u9879<\/h3>\n<\/p>\n<p><h4>1\u3001\u56fe\u6807\u6587\u4ef6\u683c\u5f0f<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528\u4e0a\u8ff0\u5de5\u5177\u65f6\uff0c\u56fe\u6807\u6587\u4ef6\u5fc5\u987b\u662f .ico \u683c\u5f0f\u3002\u5982\u679c\u4f60\u6709\u5176\u4ed6\u683c\u5f0f\u7684\u56fe\u6807\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528\u5728\u7ebf\u5de5\u5177\u6216\u56fe\u50cf\u7f16\u8f91\u8f6f\u4ef6\uff08\u5982 Photoshop\u3001GIMP\uff09\u5c06\u5176\u8f6c\u6362\u4e3a .ico \u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u8def\u5f84\u95ee\u9898<\/h4>\n<\/p>\n<p><p>\u786e\u4fdd\u4f60\u5728\u547d\u4ee4\u884c\u4e2d\u8fd0\u884c\u6253\u5305\u547d\u4ee4\u65f6\uff0c\u5f53\u524d\u76ee\u5f55\u662f\u5305\u542b\u4f60\u7684 Python \u811a\u672c\u548c\u56fe\u6807\u6587\u4ef6\u7684\u76ee\u5f55\uff0c\u6216\u8005\u4f60\u4f7f\u7528\u7edd\u5bf9\u8def\u5f84\u6765\u6307\u5b9a\u8fd9\u4e9b\u6587\u4ef6\u7684\u4f4d\u7f6e\u3002<\/p>\n<\/p>\n<p><h4>3\u3001\u4f9d\u8d56\u9879\u95ee\u9898<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u7684\u811a\u672c\u6709\u5916\u90e8\u4f9d\u8d56\u9879\uff08\u5982\u4f7f\u7528\u4e86\u7b2c\u4e09\u65b9\u5e93\uff09\uff0c\u8bf7\u786e\u4fdd\u8fd9\u4e9b\u5e93\u5df2\u7ecf\u5b89\u88c5\u5728\u4f60\u7684\u73af\u5883\u4e2d\u3002PyInstaller \u548c cx_Freeze \u4f1a\u81ea\u52a8\u68c0\u6d4b\u5e76\u6253\u5305\u8fd9\u4e9b\u4f9d\u8d56\u9879\uff0c\u4f46 py2exe \u53ef\u80fd\u9700\u8981\u4f60\u624b\u52a8\u6307\u5b9a\u4e00\u4e9b\u4f9d\u8d56\u9879\u3002<\/p>\n<\/p>\n<p><h4>4\u3001\u8c03\u8bd5\u4fe1\u606f<\/h4>\n<\/p>\n<p><p>\u5728\u6253\u5305\u8fc7\u7a0b\u4e2d\uff0c\u5982\u679c\u9047\u5230\u4efb\u4f55\u9519\u8bef\u6216\u8b66\u544a\u4fe1\u606f\uff0c\u8bf7\u4ed4\u7ec6\u9605\u8bfb\u8f93\u51fa\u7684\u65e5\u5fd7\u3002\u8fd9\u4e9b\u4fe1\u606f\u901a\u5e38\u4f1a\u6307\u793a\u51fa\u95ee\u9898\u7684\u539f\u56e0\uff0c\u5e76\u63d0\u4f9b\u89e3\u51b3\u65b9\u6848\u3002<\/p>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4f7f\u7528 PyInstaller\u3001cx_Freeze \u6216 py2exe \u7b49\u5de5\u5177\uff0c\u4f60\u53ef\u4ee5\u8f7b\u677e\u5730\u5c06 Python \u811a\u672c\u6253\u5305\u6210\u5e26\u6709\u81ea\u5b9a\u4e49\u56fe\u6807\u7684\u72ec\u7acb\u53ef\u6267\u884c\u6587\u4ef6\u3002<strong>PyInstaller \u662f\u6700\u5e38\u89c1\u4e14\u7b80\u5355\u7684\u65b9\u6cd5<\/strong>\uff0c\u5b83\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u529f\u80fd\u548c\u826f\u597d\u7684\u517c\u5bb9\u6027\uff0c\u9002\u5408\u5927\u591a\u6570\u4f7f\u7528\u573a\u666f\u3002cx_Freeze \u548c py2exe \u4e5f\u5404\u6709\u7279\u70b9\uff0c\u9002\u5408\u4e0d\u540c\u9700\u6c42\u7684\u5f00\u53d1\u8005\u3002\u5e0c\u671b\u672c\u6587\u7684\u8be6\u7ec6\u63cf\u8ff0\u80fd\u5e2e\u52a9\u4f60\u987a\u5229\u5b8c\u6210 Python \u811a\u672c\u53d8\u56fe\u6807\u7684\u4efb\u52a1\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 \/>\u5c06Python\u811a\u672c\u8f6c\u6362\u4e3a\u53ef\u6267\u884c\u6587\u4ef6\u53ef\u4ee5\u4f7f\u7528\u5de5\u5177\u5982PyInstaller\u6216cx_Freeze\u3002\u8fd9\u4e9b\u5de5\u5177\u53ef\u4ee5\u5c06Python\u4ee3\u7801\u6253\u5305\u6210\u72ec\u7acb\u7684\u53ef\u6267\u884c\u6587\u4ef6\uff0c\u7528\u6237\u65e0\u9700\u5b89\u88c5Python\u73af\u5883\u5373\u53ef\u8fd0\u884c\u3002\u4f7f\u7528\u8fd9\u4e9b\u5de5\u5177\u65f6\uff0c\u4f60\u53ea\u9700\u5728\u547d\u4ee4\u884c\u4e2d\u8fd0\u884c\u76f8\u5e94\u7684\u547d\u4ee4\uff0c\u6307\u5b9a\u4f60\u7684\u811a\u672c\u6587\u4ef6\uff0c\u5de5\u5177\u4f1a\u751f\u6210\u53ef\u6267\u884c\u6587\u4ef6\u3002<\/p>\n<p><strong>\u53ef\u4ee5\u4f7f\u7528\u54ea\u4e9b\u5de5\u5177\u5c06Python\u811a\u672c\u8f6c\u5316\u4e3a\u56fe\u6807\u5e94\u7528\uff1f<\/strong><br \/>\u9664\u4e86PyInstaller\u548ccx_Freeze\uff0c\u5176\u4ed6\u5de5\u5177\u5982py2exe\u548cNuitka\u4e5f\u53ef\u4ee5\u5c06Python\u811a\u672c\u8f6c\u6362\u4e3a\u53ef\u6267\u884c\u6587\u4ef6\u5e76\u652f\u6301\u56fe\u6807\u8bbe\u7f6e\u3002\u8fd9\u4e9b\u5de5\u5177\u5141\u8bb8\u7528\u6237\u81ea\u5b9a\u4e49\u5e94\u7528\u7a0b\u5e8f\u7684\u56fe\u6807\uff0c\u4f7f\u5176\u5728\u684c\u9762\u6216\u6587\u4ef6\u7ba1\u7406\u5668\u4e2d\u66f4\u5177\u5438\u5f15\u529b\u3002<\/p>\n<p><strong>\u5728\u8f6c\u6362\u8fc7\u7a0b\u4e2d\u5982\u4f55\u8bbe\u7f6e\u81ea\u5b9a\u4e49\u56fe\u6807\uff1f<\/strong><br \/>\u5728\u4f7f\u7528PyInstaller\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u5728\u547d\u4ee4\u4e2d\u6dfb\u52a0<code>--icon=\u56fe\u6807\u6587\u4ef6\u8def\u5f84<\/code>\u53c2\u6570\u6765\u8bbe\u7f6e\u81ea\u5b9a\u4e49\u56fe\u6807\u3002\u4f8b\u5982\uff1a<code>pyinstaller --onefile --icon=myicon.ico myscript.py<\/code>\u3002\u786e\u4fdd\u56fe\u6807\u6587\u4ef6\u683c\u5f0f\u4e3a.ico\uff0c\u5e76\u5728\u547d\u4ee4\u4e2d\u4f7f\u7528\u6587\u4ef6\u7684\u5b8c\u6574\u8def\u5f84\u6216\u76f8\u5bf9\u8def\u5f84\u3002\u8fd9\u6837\u751f\u6210\u7684\u53ef\u6267\u884c\u6587\u4ef6\u5c06\u4f1a\u5e94\u7528\u6307\u5b9a\u7684\u56fe\u6807\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u811a\u672c\u53d8\u56fe\u6807\u7684\u65b9\u6cd5\u6709\u4ee5\u4e0b\u51e0\u79cd\uff1a\u4f7f\u7528pyinstaller\u5de5\u5177\u3001\u4f7f\u7528cx_Freeze\u5de5\u5177\u3001\u4f7f\u7528py2 [&hellip;]","protected":false},"author":3,"featured_media":1159811,"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\/1159807"}],"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=1159807"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1159807\/revisions"}],"predecessor-version":[{"id":1159812,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1159807\/revisions\/1159812"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1159811"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1159807"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1159807"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1159807"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}