{"id":921137,"date":"2024-12-26T14:08:14","date_gmt":"2024-12-26T06:08:14","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/921137.html"},"modified":"2024-12-26T14:08:17","modified_gmt":"2024-12-26T06:08:17","slug":"python%e5%a6%82%e4%bd%95%e7%94%9f%e6%88%90exe%e6%96%87%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/921137.html","title":{"rendered":"python\u5982\u4f55\u751f\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\/24210236\/7518ddf8-bd2c-4d4d-bfec-1a9e37a5651a.webp\" alt=\"python\u5982\u4f55\u751f\u6210exe\u6587\u4ef6\" \/><\/p>\n<p><p> <strong>Python\u751f\u6210exe\u6587\u4ef6\u7684\u65b9\u6cd5\u4e3b\u8981\u6709\uff1a\u4f7f\u7528PyInstaller\u3001\u4f7f\u7528cx_Freeze\u3001\u4f7f\u7528Py2exe\u3002<\/strong>\u5176\u4e2d\uff0c<strong>PyInstaller<\/strong>\u662f\u6700\u5e38\u7528\u7684\u5de5\u5177\uff0c\u56e0\u4e3a\u5b83\u652f\u6301\u591a\u5e73\u53f0\u5e76\u4e14\u914d\u7f6e\u76f8\u5bf9\u7b80\u5355\u3002\u4ee5\u4e0b\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528PyInstaller\u751f\u6210exe\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001PYINSTALLER\u751f\u6210EXE\u6587\u4ef6<\/p>\n<\/p>\n<p><p>PyInstaller\u662f\u4e00\u6b3e\u529f\u80fd\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u5c06Python\u811a\u672c\u6253\u5305\u6210\u72ec\u7acb\u7684\u53ef\u6267\u884c\u6587\u4ef6\u3002\u5b83\u4e0d\u4ec5\u652f\u6301Windows\uff0c\u8fd8\u652f\u6301Linux\u548cMacOS\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528PyInstaller\u751f\u6210exe\u6587\u4ef6\u7684\u6b65\u9aa4\u3002<\/p>\n<\/p>\n<p><h3>1. \u5b89\u88c5PyInstaller<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u4f60\u9700\u8981\u786e\u4fdd\u4f60\u7684\u7cfb\u7edf\u4e2d\u5df2\u7ecf\u5b89\u88c5\u4e86Python\u73af\u5883\u3002\u7136\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7pip\u6765\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>PyInstaller\u5c06\u4f1a\u88ab\u5b89\u88c5\u5230\u4f60\u7684Python\u73af\u5883\u4e2d\uff0c\u5e76\u53ef\u901a\u8fc7\u547d\u4ee4\u884c\u8bbf\u95ee\u3002<\/p>\n<\/p>\n<p><h3>2. \u51c6\u5907Python\u811a\u672c<\/h3>\n<\/p>\n<p><p>\u5728\u4f7f\u7528PyInstaller\u4e4b\u524d\uff0c\u4f60\u9700\u8981\u51c6\u5907\u597d\u4f60\u7684Python\u811a\u672c\u3002\u5047\u8bbe\u4f60\u7684\u811a\u672c\u540d\u4e3a<code>my_script.py<\/code>\uff0c\u5e76\u4e14\u5b83\u4f4d\u4e8e\u4f60\u7684\u5de5\u4f5c\u76ee\u5f55\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>3. \u4f7f\u7528PyInstaller\u6253\u5305<\/h3>\n<\/p>\n<p><p>\u5728\u547d\u4ee4\u884c\u4e2d\uff0c\u8fdb\u5165\u4f60\u7684Python\u811a\u672c\u6240\u5728\u7684\u76ee\u5f55\uff0c\u7136\u540e\u6267\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>\u8fd9\u4e2a\u547d\u4ee4\u4f1a\u5c06\u4f60\u7684Python\u811a\u672c\u6253\u5305\u6210\u4e00\u4e2a\u5355\u72ec\u7684\u53ef\u6267\u884c\u6587\u4ef6\u3002<code>--onefile<\/code>\u53c2\u6570\u6307\u793aPyInstaller\u5c06\u6240\u6709\u4f9d\u8d56\u9879\u6253\u5305\u5230\u4e00\u4e2aexe\u6587\u4ef6\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>4. \u68c0\u67e5\u8f93\u51fa<\/h3>\n<\/p>\n<p><p>\u6253\u5305\u5b8c\u6210\u540e\uff0c\u4f60\u5c06\u5728<code>dist<\/code>\u76ee\u5f55\u4e2d\u627e\u5230\u751f\u6210\u7684exe\u6587\u4ef6\u3002\u4f60\u53ef\u4ee5\u76f4\u63a5\u5728Windows\u7cfb\u7edf\u4e0a\u8fd0\u884c\u8fd9\u4e2a\u6587\u4ef6\uff0c\u800c\u65e0\u9700\u5b89\u88c5Python\u89e3\u91ca\u5668\u3002<\/p>\n<\/p>\n<p><h3>5. \u81ea\u5b9a\u4e49\u6253\u5305\u9009\u9879<\/h3>\n<\/p>\n<p><p>PyInstaller\u63d0\u4f9b\u4e86\u8bb8\u591a\u9009\u9879\u6765\u5b9a\u5236\u4f60\u7684exe\u6587\u4ef6\u3002\u4f8b\u5982\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u6307\u5b9a\u56fe\u6807\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pyinstaller --onefile --icon=my_icon.ico my_script.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u4e9b\u6b65\u9aa4\uff0c\u4f60\u53ef\u4ee5\u8f7b\u677e\u5730\u5c06Python\u811a\u672c\u6253\u5305\u6210exe\u6587\u4ef6\uff0c\u5e76\u5206\u53d1\u7ed9\u5176\u4ed6Windows\u7528\u6237\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528CX_FREEZE\u751f\u6210EXE\u6587\u4ef6<\/p>\n<\/p>\n<p><p>cx_Freeze\u662f\u53e6\u4e00\u4e2a\u5c06Python\u811a\u672c\u8f6c\u6362\u4e3a\u53ef\u6267\u884c\u6587\u4ef6\u7684\u5de5\u5177\u3002\u5b83\u652f\u6301Windows\u3001Linux\u548cMacOS\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528cx_Freeze\u751f\u6210exe\u6587\u4ef6\u7684\u6b65\u9aa4\u3002<\/p>\n<\/p>\n<p><h3>1. \u5b89\u88c5cx_Freeze<\/h3>\n<\/p>\n<p><p>\u540c\u6837\u9700\u8981\u786e\u4fddPython\u73af\u5883\u5df2\u7ecf\u5b89\u88c5\uff0c\u7136\u540e\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>2. \u521b\u5efasetup\u811a\u672c<\/h3>\n<\/p>\n<p><p>cx_Freeze\u9700\u8981\u4e00\u4e2asetup\u811a\u672c\u6765\u6307\u5b9a\u6253\u5305\u7684\u4fe1\u606f\u3002\u521b\u5efa\u4e00\u4e2a<code>setup.py<\/code>\u6587\u4ef6\uff0c\u5185\u5bb9\u5982\u4e0b\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;MyApplication&quot;,<\/p>\n<p>    version = &quot;0.1&quot;,<\/p>\n<p>    description = &quot;My Application Description&quot;,<\/p>\n<p>    executables = [Executable(&quot;my_script.py&quot;)]<\/p>\n<p>)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3. \u8fd0\u884csetup\u811a\u672c<\/h3>\n<\/p>\n<p><p>\u5728\u547d\u4ee4\u884c\u4e2d\uff0c\u6267\u884c\u4ee5\u4e0b\u547d\u4ee4\u6765\u751f\u6210exe\u6587\u4ef6\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>cx_Freeze\u4f1a\u5728<code>build<\/code>\u76ee\u5f55\u4e2d\u751f\u6210\u53ef\u6267\u884c\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h3>4. \u81ea\u5b9a\u4e49\u9009\u9879<\/h3>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u5728<code>Executable<\/code>\u4e2d\u6dfb\u52a0\u53c2\u6570\uff0c\u6765\u5b9a\u5236\u751f\u6210\u7684\u53ef\u6267\u884c\u6587\u4ef6\uff0c\u4f8b\u5982\u8bbe\u7f6e\u56fe\u6807\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">Executable(&quot;my_script.py&quot;, icon=&quot;my_icon.ico&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>cx_Freeze\u63d0\u4f9b\u4e86\u4e00\u79cd\u7075\u6d3b\u7684\u65b9\u5f0f\u6765\u6253\u5305Python\u5e94\u7528\u7a0b\u5e8f\uff0c\u9002\u5408\u9700\u8981\u590d\u6742\u914d\u7f6e\u7684\u9879\u76ee\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528PY2EXE\u751f\u6210EXE\u6587\u4ef6<\/p>\n<\/p>\n<p><p>Py2exe\u662f\u4e00\u4e2a\u4e13\u95e8\u7528\u4e8e\u5c06Python\u811a\u672c\u8f6c\u6362\u4e3aWindows\u53ef\u6267\u884c\u6587\u4ef6\u7684\u5de5\u5177\u3002\u4ee5\u4e0b\u662f\u5176\u4f7f\u7528\u6b65\u9aa4\u3002<\/p>\n<\/p>\n<p><h3>1. \u5b89\u88c5py2exe<\/h3>\n<\/p>\n<p><p>\u9996\u5148\u5b89\u88c5py2exe\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install py2exe<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. \u521b\u5efasetup\u811a\u672c<\/h3>\n<\/p>\n<p><p>\u4e0ecx_Freeze\u7c7b\u4f3c\uff0cpy2exe\u4e5f\u9700\u8981\u4e00\u4e2asetup\u811a\u672c\u3002\u521b\u5efa\u4e00\u4e2a<code>setup.py<\/code>\u6587\u4ef6\uff0c\u5185\u5bb9\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from distutils.core import setup<\/p>\n<p>import py2exe<\/p>\n<p>setup(console=[&#39;my_script.py&#39;])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3. \u8fd0\u884csetup\u811a\u672c<\/h3>\n<\/p>\n<p><p>\u5728\u547d\u4ee4\u884c\u4e2d\uff0c\u6267\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\u53ef\u6267\u884c\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h3>4. \u81ea\u5b9a\u4e49\u9009\u9879<\/h3>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u5728<code>setup<\/code>\u51fd\u6570\u4e2d\u6dfb\u52a0\u9009\u9879\uff0c\u5b9a\u5236\u751f\u6210\u7684exe\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><p>Py2exe\u9002\u5408\u4e8eWindows\u5e73\u53f0\uff0c\u4f46\u5b83\u7684\u66f4\u65b0\u9891\u7387\u8f83\u4f4e\uff0c\u5efa\u8bae\u5728\u5176\u4ed6\u5de5\u5177\u65e0\u6cd5\u6ee1\u8db3\u9700\u6c42\u65f6\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u603b\u7ed3\u4e0e\u6ce8\u610f\u4e8b\u9879<\/p>\n<\/p>\n<p><p>\u5728\u9009\u62e9\u5de5\u5177\u65f6\uff0c<strong>PyInstaller<\/strong>\u662f\u6700\u63a8\u8350\u7684\uff0c\u56e0\u4e3a\u5b83\u652f\u6301\u7684\u5e73\u53f0\u6700\u5e7f\uff0c\u4f7f\u7528\u4e5f\u6700\u7b80\u5355\u3002<strong>cx_Freeze<\/strong>\u548c<strong>py2exe<\/strong>\u5219\u9002\u5408\u7279\u5b9a\u7684\u9700\u6c42\u548c\u5e73\u53f0\u3002<\/p>\n<\/p>\n<p><p>\u5728\u6253\u5305\u8fc7\u7a0b\u4e2d\uff0c\u9700\u8981\u6ce8\u610f\u4ee5\u4e0b\u51e0\u70b9\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u4f9d\u8d56\u7ba1\u7406<\/strong>\uff1a\u786e\u4fdd\u6240\u6709\u4f9d\u8d56\u9879\u90fd\u5728Python\u73af\u5883\u4e2d\u6b63\u786e\u5b89\u88c5\u3002<\/li>\n<li><strong>\u6d4b\u8bd5<\/strong>\uff1a\u751f\u6210exe\u6587\u4ef6\u540e\uff0c\u5728\u4e0d\u540c\u7684Windows\u7248\u672c\u4e0a\u8fdb\u884c\u6d4b\u8bd5\uff0c\u786e\u4fdd\u517c\u5bb9\u6027\u3002<\/li>\n<li><strong>\u5b89\u5168\u6027<\/strong>\uff1a\u5982\u679c\u4f60\u7684\u811a\u672c\u5305\u542b\u654f\u611f\u4fe1\u606f\uff0c\u5728\u6253\u5305\u524d\u8981\u6ce8\u610f\u4fe1\u606f\u4fdd\u62a4\u3002<\/li>\n<\/ul>\n<p><p>\u901a\u8fc7\u8fd9\u7bc7\u6587\u7ae0\uff0c\u4f60\u5e94\u8be5\u80fd\u591f\u719f\u7ec3\u5730\u5c06Python\u811a\u672c\u6253\u5305\u6210exe\u6587\u4ef6\uff0c\u5e76\u6839\u636e\u9879\u76ee\u7684\u9700\u8981\u9009\u62e9\u5408\u9002\u7684\u5de5\u5177\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u5c06\u811a\u672c\u8f6c\u6362\u4e3a\u53ef\u6267\u884c\u6587\u4ef6\uff1f<\/strong><br \/>\u5c06Python\u811a\u672c\u8f6c\u6362\u4e3a\u53ef\u6267\u884c\u6587\u4ef6\u901a\u5e38\u4f7f\u7528PyInstaller\u3001cx_Freeze\u6216py2exe\u7b49\u5de5\u5177\u3002\u4ee5PyInstaller\u4e3a\u4f8b\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u5b89\u88c5\u8be5\u5de5\u5177\u5e76\u4f7f\u7528\u547d\u4ee4\u884c\u6307\u4ee4\u201cpyinstaller your_script.py\u201d\u6765\u751f\u6210exe\u6587\u4ef6\u3002\u6b64\u8fc7\u7a0b\u4e2d\uff0c\u60a8\u53ef\u4ee5\u9009\u62e9\u4e0d\u540c\u7684\u9009\u9879\uff0c\u5982\u662f\u5426\u9690\u85cf\u63a7\u5236\u53f0\u7a97\u53e3\u6216\u6253\u5305\u4f9d\u8d56\u9879\u3002<\/p>\n<p><strong>\u751f\u6210exe\u6587\u4ef6\u540e\uff0c\u5982\u4f55\u5728\u5176\u4ed6\u8ba1\u7b97\u673a\u4e0a\u8fd0\u884c\uff1f<\/strong><br \/>\u751f\u6210\u7684exe\u6587\u4ef6\u53ef\u4ee5\u5728\u6ca1\u6709\u5b89\u88c5Python\u7684\u8ba1\u7b97\u673a\u4e0a\u8fd0\u884c\u3002\u7136\u800c\uff0c\u786e\u4fdd\u6240\u6709\u5fc5\u8981\u7684\u4f9d\u8d56\u6587\u4ef6\u90fd\u5728\u540c\u4e00\u76ee\u5f55\u4e0b\uff0c\u6216\u8005\u4f7f\u7528PyInstaller\u7684\u201c&#8211;onefile\u201d\u9009\u9879\u6765\u521b\u5efa\u5355\u4e00\u7684\u53ef\u6267\u884c\u6587\u4ef6\uff0c\u8fd9\u6837\u53ef\u4ee5\u907f\u514d\u4f9d\u8d56\u95ee\u9898\u3002\u6d4b\u8bd5\u5728\u4e0d\u540c\u73af\u5883\u4e2d\u7684\u8fd0\u884c\u60c5\u51b5\u4e5f\u662f\u4e00\u4e2a\u597d\u4e60\u60ef\u3002<\/p>\n<p><strong>\u5728\u8f6c\u6362\u8fc7\u7a0b\u4e2d\u9047\u5230\u9519\u8bef\u8be5\u5982\u4f55\u89e3\u51b3\uff1f<\/strong><br \/>\u5982\u679c\u5728\u4f7f\u7528PyInstaller\u6216\u5176\u4ed6\u5de5\u5177\u65f6\u9047\u5230\u9519\u8bef\uff0c\u9996\u5148\u68c0\u67e5\u811a\u672c\u4e2d\u662f\u5426\u6709\u8bed\u6cd5\u9519\u8bef\u6216\u672a\u5b89\u88c5\u7684\u4f9d\u8d56\u9879\u3002\u60a8\u53ef\u4ee5\u67e5\u9605\u751f\u6210\u7684\u65e5\u5fd7\u6587\u4ef6\u4ee5\u83b7\u53d6\u8be6\u7ec6\u7684\u9519\u8bef\u4fe1\u606f\u3002\u6b64\u5916\uff0c\u8bbf\u95ee\u76f8\u5173\u7684\u793e\u533a\u8bba\u575b\u6216\u6587\u6863\u53ef\u4ee5\u5e2e\u52a9\u60a8\u627e\u5230\u89e3\u51b3\u65b9\u6848\uff0c\u5e38\u89c1\u95ee\u9898\u7684\u89e3\u51b3\u65b9\u6cd5\u5f80\u5f80\u5df2\u7ecf\u88ab\u5176\u4ed6\u5f00\u53d1\u8005\u8bb0\u5f55\u4e0b\u6765\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u751f\u6210exe\u6587\u4ef6\u7684\u65b9\u6cd5\u4e3b\u8981\u6709\uff1a\u4f7f\u7528PyInstaller\u3001\u4f7f\u7528cx_Freeze\u3001\u4f7f\u7528Py2exe\u3002 [&hellip;]","protected":false},"author":3,"featured_media":921141,"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\/921137"}],"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=921137"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/921137\/revisions"}],"predecessor-version":[{"id":921142,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/921137\/revisions\/921142"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/921141"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=921137"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=921137"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=921137"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}