{"id":1179369,"date":"2025-01-15T18:22:44","date_gmt":"2025-01-15T10:22:44","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1179369.html"},"modified":"2025-01-15T18:22:47","modified_gmt":"2025-01-15T10:22:47","slug":"%e5%a6%82%e4%bd%95%e6%8a%8apython%e6%ba%90%e7%a0%81%e5%81%9a%e6%88%90exe","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1179369.html","title":{"rendered":"\u5982\u4f55\u628apython\u6e90\u7801\u505a\u6210exe"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25113821\/10e62abb-8e9f-4ea4-aa2b-bb1b42f0f30c.webp\" alt=\"\u5982\u4f55\u628apython\u6e90\u7801\u505a\u6210exe\" \/><\/p>\n<p><p> <strong>\u8981\u628aPython\u6e90\u7801\u6253\u5305\u6210exe\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528\u5de5\u5177\u5982PyInstaller\u3001cx_Freeze\u3001Py2exe\u7b49\u3002<\/strong> \u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u5de5\u5177\u5c06Python\u6e90\u7801\u6253\u5305\u6210exe\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001PyInstaller<\/p>\n<\/p>\n<p><p>PyInstaller\u662f\u4e00\u4e2a\u975e\u5e38\u6d41\u884c\u7684\u6253\u5305\u5de5\u5177\uff0c\u5b83\u53ef\u4ee5\u5c06Python\u5e94\u7528\u7a0b\u5e8f\u6253\u5305\u6210\u72ec\u7acb\u7684\u53ef\u6267\u884c\u6587\u4ef6\u3002<strong>\u4f7f\u7528PyInstaller\u7684\u4e3b\u8981\u6b65\u9aa4\u5305\u62ec\uff1a\u5b89\u88c5PyInstaller\u3001\u51c6\u5907Python\u6e90\u7801\u3001\u8fd0\u884cPyInstaller\u547d\u4ee4\u3002<\/strong><\/p>\n<\/p>\n<p><h3>\u5b89\u88c5PyInstaller<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u4f60\u9700\u8981\u5b89\u88c5PyInstaller\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528pip\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><h3>\u51c6\u5907Python\u6e90\u7801<\/h3>\n<\/p>\n<p><p>\u786e\u4fdd\u4f60\u7684Python\u6e90\u7801\u6587\u4ef6\u51c6\u5907\u597d\u3002\u5047\u8bbe\u4f60\u7684Python\u6e90\u7801\u6587\u4ef6\u662f<code>m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n.py<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u8fd0\u884cPyInstaller\u547d\u4ee4<\/h3>\n<\/p>\n<p><p>\u6253\u5f00\u547d\u4ee4\u884c\u6216\u7ec8\u7aef\uff0c\u5bfc\u822a\u5230\u5305\u542b<code>main.py<\/code>\u6587\u4ef6\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 main.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>--onefile<\/code>\u9009\u9879\u544a\u8bc9PyInstaller\u5c06\u6240\u6709\u6587\u4ef6\u6253\u5305\u6210\u4e00\u4e2a\u5355\u72ec\u7684\u53ef\u6267\u884c\u6587\u4ef6\u3002\u8fd0\u884c\u8fd9\u4e2a\u547d\u4ee4\u540e\uff0cPyInstaller\u4f1a\u751f\u6210\u4e00\u4e2a<code>dist<\/code>\u76ee\u5f55\uff0c\u91cc\u9762\u5305\u542b\u6253\u5305\u597d\u7684exe\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h3>\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/h3>\n<\/p>\n<p><p>PyInstaller\u5728\u6253\u5305\u65f6\u4f1a\u81ea\u52a8\u5206\u6790\u4f60\u7684\u6e90\u7801\u6587\u4ef6\uff0c\u627e\u5230\u6240\u6709\u7684\u4f9d\u8d56\u9879\uff0c\u5e76\u5c06\u5b83\u4eec\u4e00\u5e76\u6253\u5305\u5230\u4e00\u4e2a\u53ef\u6267\u884c\u6587\u4ef6\u4e2d\u3002\u8fd9\u4e2a\u8fc7\u7a0b\u5305\u62ec\u4ee5\u4e0b\u51e0\u4e2a\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u5206\u6790\u6e90\u7801\u6587\u4ef6<\/strong>\uff1aPyInstaller\u9996\u5148\u4f1a\u5206\u6790\u4f60\u7684\u6e90\u7801\u6587\u4ef6\uff0c\u627e\u5230\u6240\u6709\u7684\u5bfc\u5165\u6a21\u5757\u548c\u4f9d\u8d56\u9879\u3002<\/li>\n<li><strong>\u6536\u96c6\u4f9d\u8d56\u9879<\/strong>\uff1a\u63a5\u7740\uff0cPyInstaller\u4f1a\u6536\u96c6\u6240\u6709\u9700\u8981\u7684\u6a21\u5757\u548c\u5e93\u6587\u4ef6\uff0c\u5305\u62ec\u4f60\u4ee3\u7801\u4e2d\u5bfc\u5165\u7684\u6807\u51c6\u5e93\u6a21\u5757\u548c\u7b2c\u4e09\u65b9\u6a21\u5757\u3002<\/li>\n<li><strong>\u521b\u5efa\u53ef\u6267\u884c\u6587\u4ef6<\/strong>\uff1a\u6700\u540e\uff0cPyInstaller\u4f1a\u5c06\u6240\u6709\u6536\u96c6\u5230\u7684\u6587\u4ef6\u6253\u5305\u6210\u4e00\u4e2a\u72ec\u7acb\u7684\u53ef\u6267\u884c\u6587\u4ef6\u3002\u8fd9\u4e2a\u53ef\u6267\u884c\u6587\u4ef6\u53ef\u4ee5\u5728\u6ca1\u6709Python\u89e3\u91ca\u5668\u7684\u7cfb\u7edf\u4e0a\u8fd0\u884c\u3002<\/li>\n<\/ol>\n<p><p>\u4e8c\u3001cx_Freeze<\/p>\n<\/p>\n<p><p>cx_Freeze\u662f\u53e6\u4e00\u4e2a\u6d41\u884c\u7684\u6253\u5305\u5de5\u5177\uff0c\u5b83\u4e5f\u53ef\u4ee5\u5c06Python\u5e94\u7528\u7a0b\u5e8f\u6253\u5305\u6210\u72ec\u7acb\u7684\u53ef\u6267\u884c\u6587\u4ef6\u3002<strong>\u4f7f\u7528cx_Freeze\u7684\u4e3b\u8981\u6b65\u9aa4\u5305\u62ec\uff1a\u5b89\u88c5cx_Freeze\u3001\u7f16\u5199setup\u811a\u672c\u3001\u8fd0\u884csetup\u811a\u672c\u3002<\/strong><\/p>\n<\/p>\n<p><h3>\u5b89\u88c5cx_Freeze<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u4f60\u9700\u8981\u5b89\u88c5cx_Freeze\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528pip\u6765\u5b89\u88c5\u5b83\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>\u7f16\u5199setup\u811a\u672c<\/h3>\n<\/p>\n<p><p>\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a<code>setup.py<\/code>\u7684\u6587\u4ef6\uff0c\u5e76\u5728\u5176\u4e2d\u7f16\u5199\u6253\u5305\u811a\u672c\u3002\u4f8b\u5982\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;my_program&quot;,<\/p>\n<p>    version = &quot;0.1&quot;,<\/p>\n<p>    description = &quot;My Python Program&quot;,<\/p>\n<p>    executables = [Executable(&quot;main.py&quot;)]<\/p>\n<p>)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u8fd0\u884csetup\u811a\u672c<\/h3>\n<\/p>\n<p><p>\u6253\u5f00\u547d\u4ee4\u884c\u6216\u7ec8\u7aef\uff0c\u5bfc\u822a\u5230\u5305\u542b<code>setup.py<\/code>\u6587\u4ef6\u7684\u76ee\u5f55\uff0c\u7136\u540e\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>\u8fd0\u884c\u8fd9\u4e2a\u547d\u4ee4\u540e\uff0ccx_Freeze\u4f1a\u751f\u6210\u4e00\u4e2a<code>build<\/code>\u76ee\u5f55\uff0c\u91cc\u9762\u5305\u542b\u6253\u5305\u597d\u7684exe\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001Py2exe<\/p>\n<\/p>\n<p><p>Py2exe\u662f\u4e00\u4e2a\u4e13\u95e8\u7528\u4e8e\u5c06Python\u5e94\u7528\u7a0b\u5e8f\u8f6c\u6362\u6210Windows\u53ef\u6267\u884c\u6587\u4ef6\u7684\u5de5\u5177\u3002<strong>\u4f7f\u7528Py2exe\u7684\u4e3b\u8981\u6b65\u9aa4\u5305\u62ec\uff1a\u5b89\u88c5Py2exe\u3001\u7f16\u5199setup\u811a\u672c\u3001\u8fd0\u884csetup\u811a\u672c\u3002<\/strong><\/p>\n<\/p>\n<p><h3>\u5b89\u88c5Py2exe<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u4f60\u9700\u8981\u5b89\u88c5Py2exe\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528pip\u6765\u5b89\u88c5\u5b83\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install py2exe<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u7f16\u5199setup\u811a\u672c<\/h3>\n<\/p>\n<p><p>\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a<code>setup.py<\/code>\u7684\u6587\u4ef6\uff0c\u5e76\u5728\u5176\u4e2d\u7f16\u5199\u6253\u5305\u811a\u672c\u3002\u4f8b\u5982\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;main.py&#39;])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u8fd0\u884csetup\u811a\u672c<\/h3>\n<\/p>\n<p><p>\u6253\u5f00\u547d\u4ee4\u884c\u6216\u7ec8\u7aef\uff0c\u5bfc\u822a\u5230\u5305\u542b<code>setup.py<\/code>\u6587\u4ef6\u7684\u76ee\u5f55\uff0c\u7136\u540e\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>\u8fd0\u884c\u8fd9\u4e2a\u547d\u4ee4\u540e\uff0cPy2exe\u4f1a\u751f\u6210\u4e00\u4e2a<code>dist<\/code>\u76ee\u5f55\uff0c\u91cc\u9762\u5305\u542b\u6253\u5305\u597d\u7684exe\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u5728\u5c06Python\u6e90\u7801\u6253\u5305\u6210exe\u6587\u4ef6\u65f6\uff0c\u9009\u62e9\u5408\u9002\u7684\u5de5\u5177\u975e\u5e38\u91cd\u8981\u3002<strong>PyInstaller\u3001cx_Freeze\u548cPy2exe\u90fd\u662f\u4e0d\u9519\u7684\u9009\u62e9<\/strong>\uff0c\u5b83\u4eec\u5404\u6709\u4f18\u7f3a\u70b9\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u4f7f\u7528\u3002\u65e0\u8bba\u4f60\u9009\u62e9\u54ea\u4e2a\u5de5\u5177\uff0c\u90fd\u9700\u8981\u6309\u7167\u4e0a\u8ff0\u6b65\u9aa4\u5b89\u88c5\u5de5\u5177\u3001\u7f16\u5199setup\u811a\u672c\uff08\u5982\u679c\u9700\u8981\uff09\u3001\u8fd0\u884c\u6253\u5305\u547d\u4ee4\uff0c\u5373\u53ef\u751f\u6210\u53ef\u6267\u884c\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h3>PyInstaller\u7684\u4f18\u52bf<\/h3>\n<\/p>\n<ul>\n<li><strong>\u8de8\u5e73\u53f0\u652f\u6301<\/strong>\uff1aPyInstaller\u652f\u6301Windows\u3001Linux\u548cmacOS\uff0c\u53ef\u4ee5\u6253\u5305\u8de8\u5e73\u53f0\u7684\u53ef\u6267\u884c\u6587\u4ef6\u3002<\/li>\n<li><strong>\u7b80\u5355\u6613\u7528<\/strong>\uff1aPyInstaller\u7684\u547d\u4ee4\u884c\u754c\u9762\u975e\u5e38\u7b80\u5355\uff0c\u901a\u5e38\u53ea\u9700\u8981\u4e00\u4e2a\u547d\u4ee4\u5373\u53ef\u5b8c\u6210\u6253\u5305\u3002<\/li>\n<li><strong>\u5f3a\u5927\u7684\u4f9d\u8d56\u5206\u6790<\/strong>\uff1aPyInstaller\u80fd\u591f\u81ea\u52a8\u5206\u6790\u5e76\u6536\u96c6\u6240\u6709\u7684\u4f9d\u8d56\u9879\uff0c\u5305\u62ec\u52a8\u6001\u5e93\u548c\u8d44\u6e90\u6587\u4ef6\u3002<\/li>\n<\/ul>\n<p><h3>cx_Freeze\u7684\u4f18\u52bf<\/h3>\n<\/p>\n<ul>\n<li><strong>\u9ad8\u5ea6\u53ef\u914d\u7f6e<\/strong>\uff1acx_Freeze\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u914d\u7f6e\u9009\u9879\uff0c\u53ef\u4ee5\u6839\u636e\u9700\u6c42\u81ea\u5b9a\u4e49\u6253\u5305\u8fc7\u7a0b\u3002<\/li>\n<li><strong>\u652f\u6301\u591a\u79cdPython\u7248\u672c<\/strong>\uff1acx_Freeze\u652f\u6301Python 3.x\u7248\u672c\uff0c\u53ef\u4ee5\u6253\u5305\u73b0\u4ee3\u7684Python\u5e94\u7528\u7a0b\u5e8f\u3002<\/li>\n<li><strong>\u751f\u6210\u72ec\u7acb\u7684\u76ee\u5f55\u7ed3\u6784<\/strong>\uff1acx_Freeze\u751f\u6210\u7684\u53ef\u6267\u884c\u6587\u4ef6\u5305\u542b\u4e00\u4e2a\u72ec\u7acb\u7684\u76ee\u5f55\u7ed3\u6784\uff0c\u4fbf\u4e8e\u5206\u53d1\u548c\u90e8\u7f72\u3002<\/li>\n<\/ul>\n<p><h3>Py2exe\u7684\u4f18\u52bf<\/h3>\n<\/p>\n<ul>\n<li><strong>\u4e13\u4e3aWindows\u8bbe\u8ba1<\/strong>\uff1aPy2exe\u4e13\u95e8\u7528\u4e8e\u5c06Python\u5e94\u7528\u7a0b\u5e8f\u6253\u5305\u6210Windows\u53ef\u6267\u884c\u6587\u4ef6\uff0c\u652f\u6301Windows\u5e73\u53f0\u7684\u7279\u6027\u3002<\/li>\n<li><strong>\u7b80\u5355\u7684setup\u811a\u672c<\/strong>\uff1aPy2exe\u7684setup\u811a\u672c\u975e\u5e38\u7b80\u5355\uff0c\u9002\u5408\u521d\u5b66\u8005\u4f7f\u7528\u3002<\/li>\n<li><strong>\u517c\u5bb9\u6027\u597d<\/strong>\uff1aPy2exe\u4e0e\u8bb8\u591a\u7b2c\u4e09\u65b9\u5e93\u517c\u5bb9\uff0c\u53ef\u4ee5\u6253\u5305\u4f9d\u8d56\u4e8e\u8fd9\u4e9b\u5e93\u7684\u5e94\u7528\u7a0b\u5e8f\u3002<\/li>\n<\/ul>\n<p><p>\u4e94\u3001\u5177\u4f53\u793a\u4f8b<\/p>\n<\/p>\n<p><p>\u4e3a\u4e86\u66f4\u597d\u5730\u7406\u89e3\u5982\u4f55\u5c06Python\u6e90\u7801\u6253\u5305\u6210exe\u6587\u4ef6\uff0c\u4e0b\u9762\u901a\u8fc7\u4e00\u4e2a\u5177\u4f53\u7684\u793a\u4f8b\u6765\u6f14\u793a\u5982\u4f55\u4f7f\u7528PyInstaller\u3001cx_Freeze\u548cPy2exe\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b\u5e94\u7528\u7a0b\u5e8f<\/h3>\n<\/p>\n<p><p>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u7b80\u5355\u7684Python\u5e94\u7528\u7a0b\u5e8f<code>main.py<\/code>\uff0c\u5185\u5bb9\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<p>def main():<\/p>\n<p>    print(&quot;Hello, World!&quot;)<\/p>\n<p>    input(&quot;Press Enter to exit...&quot;)<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u4f7f\u7528PyInstaller<\/h4>\n<\/p>\n<ol>\n<li><strong>\u5b89\u88c5PyInstaller<\/strong>\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-bash\">pip install pyinstaller<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u6253\u5305\u5e94\u7528\u7a0b\u5e8f<\/strong>\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-bash\">pyinstaller --onefile main.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u8fd0\u884c\u53ef\u6267\u884c\u6587\u4ef6<\/strong>\uff1a<\/li>\n<\/ol>\n<p><p>\u5728<code>dist<\/code>\u76ee\u5f55\u4e0b\u627e\u5230<code>main.exe<\/code>\uff0c\u53cc\u51fb\u8fd0\u884c\u5b83\uff0c\u4f60\u5c06\u770b\u5230\u63a7\u5236\u53f0\u8f93\u51fa\u201cHello, World!\u201d\u5e76\u7b49\u5f85\u7528\u6237\u8f93\u5165\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528cx_Freeze<\/h4>\n<\/p>\n<ol>\n<li><strong>\u5b89\u88c5cx_Freeze<\/strong>\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-bash\">pip install cx_Freeze<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u7f16\u5199setup\u811a\u672c<\/strong>\uff1a<\/li>\n<\/ol>\n<p><p>\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a<code>setup.py<\/code>\u7684\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;hello_world&quot;,<\/p>\n<p>    version = &quot;0.1&quot;,<\/p>\n<p>    description = &quot;Hello World Program&quot;,<\/p>\n<p>    executables = [Executable(&quot;main.py&quot;)]<\/p>\n<p>)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u6253\u5305\u5e94\u7528\u7a0b\u5e8f<\/strong>\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-bash\">python setup.py build<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"4\">\n<li><strong>\u8fd0\u884c\u53ef\u6267\u884c\u6587\u4ef6<\/strong>\uff1a<\/li>\n<\/ol>\n<p><p>\u5728<code>build<\/code>\u76ee\u5f55\u4e0b\u627e\u5230\u751f\u6210\u7684\u53ef\u6267\u884c\u6587\u4ef6\uff0c\u53cc\u51fb\u8fd0\u884c\u5b83\uff0c\u4f60\u5c06\u770b\u5230\u63a7\u5236\u53f0\u8f93\u51fa\u201cHello, World!\u201d\u5e76\u7b49\u5f85\u7528\u6237\u8f93\u5165\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528Py2exe<\/h4>\n<\/p>\n<ol>\n<li><strong>\u5b89\u88c5Py2exe<\/strong>\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-bash\">pip install py2exe<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u7f16\u5199setup\u811a\u672c<\/strong>\uff1a<\/li>\n<\/ol>\n<p><p>\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a<code>setup.py<\/code>\u7684\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;main.py&#39;])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u6253\u5305\u5e94\u7528\u7a0b\u5e8f<\/strong>\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-bash\">python setup.py py2exe<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"4\">\n<li><strong>\u8fd0\u884c\u53ef\u6267\u884c\u6587\u4ef6<\/strong>\uff1a<\/li>\n<\/ol>\n<p><p>\u5728<code>dist<\/code>\u76ee\u5f55\u4e0b\u627e\u5230<code>main.exe<\/code>\uff0c\u53cc\u51fb\u8fd0\u884c\u5b83\uff0c\u4f60\u5c06\u770b\u5230\u63a7\u5236\u53f0\u8f93\u51fa\u201cHello, World!\u201d\u5e76\u7b49\u5f85\u7528\u6237\u8f93\u5165\u3002<\/p>\n<\/p>\n<p><h3>\u7ed3\u8bba<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u793a\u4f8b\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230\uff0c<strong>\u4f7f\u7528PyInstaller\u3001cx_Freeze\u548cPy2exe\u6253\u5305Python\u6e90\u7801\u6210exe\u6587\u4ef6\u7684\u8fc7\u7a0b\u5e76\u4e0d\u590d\u6742<\/strong>\u3002\u6bcf\u4e2a\u5de5\u5177\u90fd\u6709\u5176\u72ec\u7279\u7684\u4f18\u52bf\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u5de5\u5177\u3002\u65e0\u8bba\u4f7f\u7528\u54ea\u79cd\u5de5\u5177\uff0c\u90fd\u9700\u8981\u786e\u4fdd\u5b89\u88c5\u6b63\u786e\u3001\u7f16\u5199\u6b63\u786e\u7684setup\u811a\u672c\uff08\u5982\u679c\u9700\u8981\uff09\uff0c\u5e76\u8fd0\u884c\u76f8\u5e94\u7684\u6253\u5305\u547d\u4ee4\uff0c\u5373\u53ef\u751f\u6210\u53ef\u6267\u884c\u6587\u4ef6\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 \/>\u5c06Python\u7a0b\u5e8f\u6253\u5305\u6210exe\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528PyInstaller\u3001cx_Freeze\u6216py2exe\u7b49\u5de5\u5177\u3002\u8fd9\u4e9b\u5de5\u5177\u53ef\u4ee5\u5c06Python\u89e3\u91ca\u5668\u548c\u4f60\u7684\u4ee3\u7801\u6253\u5305\u5728\u4e00\u8d77\uff0c\u4f7f\u5f97\u7528\u6237\u65e0\u9700\u5b89\u88c5Python\u5373\u53ef\u8fd0\u884c\u4f60\u7684\u7a0b\u5e8f\u3002\u5177\u4f53\u6b65\u9aa4\u901a\u5e38\u5305\u62ec\u5b89\u88c5\u76f8\u5173\u5de5\u5177\u3001\u7f16\u5199\u6253\u5305\u914d\u7f6e\uff0c\u5e76\u6267\u884c\u6253\u5305\u547d\u4ee4\u3002<\/p>\n<p><strong>\u4f7f\u7528PyInstaller\u6253\u5305exe\u6587\u4ef6\u7684\u6b65\u9aa4\u6709\u54ea\u4e9b\uff1f<\/strong><br \/>\u4f7f\u7528PyInstaller\u6253\u5305exe\u6587\u4ef6\u7684\u8fc7\u7a0b\u76f8\u5bf9\u7b80\u5355\u3002\u9996\u5148\uff0c\u786e\u4fdd\u4f60\u7684\u7cfb\u7edf\u4e2d\u5df2\u5b89\u88c5Python\u548cpip\u3002\u63a5\u4e0b\u6765\uff0c\u901a\u8fc7\u547d\u4ee4\u884c\u5b89\u88c5PyInstaller\u3002\u7136\u540e\uff0c\u5bfc\u822a\u5230\u4f60\u7684Python\u811a\u672c\u6240\u5728\u7684\u76ee\u5f55\uff0c\u8fd0\u884c<code>pyinstaller your_script.py<\/code>\u547d\u4ee4\u3002\u751f\u6210\u7684exe\u6587\u4ef6\u5c06\u4f4d\u4e8edist\u6587\u4ef6\u5939\u4e2d\uff0c\u901a\u5e38\u8fd8\u4f1a\u5305\u542b\u5176\u4ed6\u4f9d\u8d56\u6587\u4ef6\u3002<\/p>\n<p><strong>\u6253\u5305\u540e\u7684exe\u6587\u4ef6\u5728\u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\u4e0a\u662f\u5426\u517c\u5bb9\uff1f<\/strong><br \/>\u6253\u5305\u540e\u7684exe\u6587\u4ef6\u901a\u5e38\u53ea\u80fd\u5728Windows\u64cd\u4f5c\u7cfb\u7edf\u4e0a\u8fd0\u884c\uff0c\u56e0\u4e3aexe\u662fWindows\u7279\u6709\u7684\u53ef\u6267\u884c\u6587\u4ef6\u683c\u5f0f\u3002\u5982\u679c\u9700\u8981\u5728macOS\u6216Linux\u4e0a\u8fd0\u884c\uff0c\u5efa\u8bae\u4f7f\u7528\u5bf9\u5e94\u5e73\u53f0\u7684\u6253\u5305\u5de5\u5177\uff0c\u5982py2app\uff08\u7528\u4e8emacOS\uff09\u6216\u4f7f\u7528Docker\u5bb9\u5668\u6765\u5b9e\u73b0\u8de8\u5e73\u53f0\u517c\u5bb9\u6027\u3002<\/p>\n<p><strong>\u5982\u4f55\u89e3\u51b3\u6253\u5305\u8fc7\u7a0b\u4e2d\u9047\u5230\u7684\u5e38\u89c1\u9519\u8bef\uff1f<\/strong><br \/>\u5728\u6253\u5305\u8fc7\u7a0b\u4e2d\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u7f3a\u5931\u6a21\u5757\u6216\u4f9d\u8d56\u9879\u7684\u95ee\u9898\u3002\u8fd9\u65f6\u53ef\u4ee5\u901a\u8fc7\u67e5\u770bPyInstaller\u751f\u6210\u7684\u8b66\u544a\u4fe1\u606f\uff0c\u624b\u52a8\u6dfb\u52a0\u7f3a\u5931\u7684\u6a21\u5757\u5230.spec\u6587\u4ef6\u4e2d\uff0c\u6216\u4f7f\u7528<code>--hidden-import<\/code>\u9009\u9879\u6765\u6307\u5b9a\u9700\u8981\u5305\u542b\u7684\u6a21\u5757\u3002\u6b64\u5916\uff0c\u786e\u4fdd\u4f7f\u7528\u7684Python\u7248\u672c\u4e0e\u5e93\u7248\u672c\u517c\u5bb9\u4e5f\u5f88\u91cd\u8981\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u628aPython\u6e90\u7801\u6253\u5305\u6210exe\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528\u5de5\u5177\u5982PyInstaller\u3001cx_Freeze\u3001Py2exe\u7b49 [&hellip;]","protected":false},"author":3,"featured_media":1179373,"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\/1179369"}],"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=1179369"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1179369\/revisions"}],"predecessor-version":[{"id":1179374,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1179369\/revisions\/1179374"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1179373"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1179369"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1179369"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1179369"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}