{"id":1095311,"date":"2025-01-08T14:49:56","date_gmt":"2025-01-08T06:49:56","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1095311.html"},"modified":"2025-01-08T14:49:59","modified_gmt":"2025-01-08T06:49:59","slug":"%e5%a6%82%e4%bd%95%e6%8a%8apython%e4%bb%a3%e7%a0%81%e8%bd%ac%e5%8c%96%e4%b8%baexe%e6%a0%bc%e5%bc%8f-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1095311.html","title":{"rendered":"\u5982\u4f55\u628apython\u4ee3\u7801\u8f6c\u5316\u4e3aexe\u683c\u5f0f"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24211032\/7fb218ba-f638-4106-9a6b-5c186869b239.webp\" alt=\"\u5982\u4f55\u628apython\u4ee3\u7801\u8f6c\u5316\u4e3aexe\u683c\u5f0f\" \/><\/p>\n<p><p> \u5c06Python\u4ee3\u7801\u8f6c\u5316\u4e3aexe\u683c\u5f0f\u7684\u65b9\u6cd5\u4e3b\u8981\u6709\u4ee5\u4e0b\u51e0\u79cd\uff1a<strong>\u4f7f\u7528PyInstaller\u3001\u4f7f\u7528cx_Freeze\u3001\u4f7f\u7528py2exe\u3001\u4f7f\u7528auto-py-to-exe<\/strong>\u3002\u5176\u4e2d\uff0c<strong>PyInstaller<\/strong>\u662f\u6700\u5e38\u7528\u548c\u63a8\u8350\u7684\u65b9\u6cd5\uff0c\u56e0\u4e3a\u5b83\u652f\u6301\u591a\u5e73\u53f0\uff0c\u5e76\u4e14\u4f7f\u7528\u76f8\u5bf9\u7b80\u5355\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528PyInstaller\u5c06Python\u4ee3\u7801\u8f6c\u6362\u4e3aexe\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528PyInstaller<\/h3>\n<\/p>\n<p><h4>1\u3001\u5b89\u88c5PyInstaller<\/h4>\n<\/p>\n<p><p>\u8981\u4f7f\u7528PyInstaller\uff0c\u9996\u5148\u9700\u8981\u5b89\u88c5\u5b83\u3002\u53ef\u4ee5\u901a\u8fc7pip\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<p><h4>2\u3001\u751f\u6210EXE\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u5728\u547d\u4ee4\u884c\u4e2d\u4f7f\u7528PyInstaller\u547d\u4ee4\u5c06Python\u4ee3\u7801\u8f6c\u6362\u4e3aexe\u6587\u4ef6\u3002\u5047\u8bbe\u4f60\u7684Python\u811a\u672c\u662f<code>myscript.py<\/code>\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pyinstaller --onefile myscript.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u547d\u4ee4\u4f1a\u5728\u5f53\u524d\u76ee\u5f55\u4e0b\u751f\u6210\u4e00\u4e2a<code>dist<\/code>\u6587\u4ef6\u5939\uff0c\u91cc\u9762\u5305\u542b\u4e00\u4e2a\u72ec\u7acb\u7684\u53ef\u6267\u884c\u6587\u4ef6<code>myscript.exe<\/code>\u3002<code>--onefile<\/code>\u53c2\u6570\u8868\u793a\u5c06\u6240\u6709\u4f9d\u8d56\u6253\u5305\u5230\u4e00\u4e2aexe\u6587\u4ef6\u4e2d\u3002<\/p>\n<\/p>\n<p><h4>3\u3001\u914d\u7f6ePyInstaller<\/h4>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u901a\u8fc7\u521b\u5efa\u4e00\u4e2a.spec\u6587\u4ef6\u6765\u66f4\u8be6\u7ec6\u5730\u914d\u7f6ePyInstaller\u3002<code>spec<\/code>\u6587\u4ef6\u662fPyInstaller\u7684\u914d\u7f6e\u6587\u4ef6\uff0c\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\u53ef\u4ee5\u751f\u6210\u4e00\u4e2a\u9ed8\u8ba4\u7684.spec\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pyinstaller myscript.py --onefile --name MyExecutable<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\u4f60\u53ef\u4ee5\u7f16\u8f91\u8fd9\u4e2a.spec\u6587\u4ef6\uff0c\u6dfb\u52a0\u4f60\u9700\u8981\u7684\u914d\u7f6e\uff0c\u6bd4\u5982\u56fe\u6807\u3001\u9644\u52a0\u7684\u6570\u636e\u6587\u4ef6\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># myscript.spec<\/p>\n<h2><strong>-*- mode: python -*-<\/strong><\/h2>\n<p>block_cipher = None<\/p>\n<p>a = Analysis(<\/p>\n<p>    [&#39;myscript.py&#39;],<\/p>\n<p>    pathex=[],<\/p>\n<p>    binaries=[],<\/p>\n<p>    datas=[],<\/p>\n<p>    hiddenimports=[],<\/p>\n<p>    hookspath=[],<\/p>\n<p>    runtime_hooks=[],<\/p>\n<p>    excludes=[],<\/p>\n<p>    win_no_prefer_redirects=False,<\/p>\n<p>    win_private_assemblies=False,<\/p>\n<p>    cipher=block_cipher,<\/p>\n<p>)<\/p>\n<p>pyz = PYZ(a.pure, a.zipped_data, cipher=block_cipher)<\/p>\n<p>exe = EXE(<\/p>\n<p>    pyz,<\/p>\n<p>    a.scripts,<\/p>\n<p>    [],<\/p>\n<p>    exclude_binaries=True,<\/p>\n<p>    name=&#39;MyExecutable&#39;,<\/p>\n<p>    debug=False,<\/p>\n<p>    bootloader_ignore_signals=False,<\/p>\n<p>    strip=False,<\/p>\n<p>    upx=True,<\/p>\n<p>    upx_exclude=[],<\/p>\n<p>    runtime_tmpdir=None,<\/p>\n<p>    console=True,<\/p>\n<p>)<\/p>\n<p>coll = COLLECT(<\/p>\n<p>    exe,<\/p>\n<p>    a.binaries,<\/p>\n<p>    a.zipfiles,<\/p>\n<p>    a.datas,<\/p>\n<p>    strip=False,<\/p>\n<p>    upx=True,<\/p>\n<p>    upx_exclude=[],<\/p>\n<p>    name=&#39;MyExecutable&#39;,<\/p>\n<p>)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u751f\u6210.spec\u6587\u4ef6\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u6765\u521b\u5efaexe\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pyinstaller myscript.spec<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528cx_Freeze<\/h3>\n<\/p>\n<p><h4>1\u3001\u5b89\u88c5cx_Freeze<\/h4>\n<\/p>\n<p><p>\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><h4>2\u3001\u521b\u5efasetup.py\u6587\u4ef6<\/h4>\n<\/p>\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;MyProgram&quot;,<\/p>\n<p>    version = &quot;0.1&quot;,<\/p>\n<p>    description = &quot;My Python program&quot;,<\/p>\n<p>    executables = [Executable(&quot;myscript.py&quot;)]<\/p>\n<p>)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u751f\u6210EXE\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\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>\u751f\u6210\u7684\u53ef\u6267\u884c\u6587\u4ef6\u4f1a\u5728<code>build<\/code>\u6587\u4ef6\u5939\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528py2exe<\/h3>\n<\/p>\n<p><h4>1\u3001\u5b89\u88c5py2exe<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528pip\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><h4>2\u3001\u521b\u5efasetup.py\u6587\u4ef6<\/h4>\n<\/p>\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(<\/p>\n<p>    console=[&#39;myscript.py&#39;]<\/p>\n<p>)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u751f\u6210EXE\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\u751f\u6210exe\u6587\u4ef6\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>\u751f\u6210\u7684\u53ef\u6267\u884c\u6587\u4ef6\u4f1a\u5728<code>dist<\/code>\u6587\u4ef6\u5939\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528auto-py-to-exe<\/h3>\n<\/p>\n<p><h4>1\u3001\u5b89\u88c5auto-py-to-exe<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528pip\u5b89\u88c5auto-py-to-exe\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install auto-py-to-exe<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u8fd0\u884cauto-py-to-exe<\/h4>\n<\/p>\n<p><p>\u5728\u547d\u4ee4\u884c\u4e2d\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\u542f\u52a8auto-py-to-exe\u56fe\u5f62\u754c\u9762\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">auto-py-to-exe<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u914d\u7f6e\u548c\u751f\u6210EXE\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u5728\u56fe\u5f62\u754c\u9762\u4e2d\u9009\u62e9\u4f60\u7684Python\u811a\u672c\uff0c\u914d\u7f6e\u76f8\u5173\u9009\u9879\uff08\u5982\u662f\u5426\u9700\u8981\u4e00\u4e2a\u72ec\u7acb\u7684exe\u6587\u4ef6\u3001\u662f\u5426\u9700\u8981\u56fe\u6807\u7b49\uff09\uff0c\u7136\u540e\u70b9\u51fb\u201c\u8f6c\u6362\u201d\u6309\u94ae\u751f\u6210exe\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5c06Python\u4ee3\u7801\u8f6c\u5316\u4e3aexe\u683c\u5f0f\u7684\u4e3b\u8981\u65b9\u6cd5\u5305\u62ec\uff1a<strong>\u4f7f\u7528PyInstaller\u3001\u4f7f\u7528cx_Freeze\u3001\u4f7f\u7528py2exe\u3001\u4f7f\u7528auto-py-to-exe<\/strong>\u3002\u5176\u4e2d\uff0cPyInstaller\u662f\u6700\u5e38\u7528\u7684\u65b9\u6cd5\uff0c\u56e0\u4e3a\u5b83\u652f\u6301\u591a\u5e73\u53f0\uff0c\u5e76\u4e14\u4f7f\u7528\u76f8\u5bf9\u7b80\u5355\u3002\u901a\u8fc7\u5b89\u88c5PyInstaller\u5e76\u8fd0\u884c\u76f8\u5e94\u7684\u547d\u4ee4\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u5c06Python\u811a\u672c\u8f6c\u6362\u4e3a\u72ec\u7acb\u7684\u53ef\u6267\u884c\u6587\u4ef6\u3002\u5982\u679c\u9700\u8981\u66f4\u590d\u6742\u7684\u914d\u7f6e\uff0c\u53ef\u4ee5\u901a\u8fc7\u521b\u5efa.spec\u6587\u4ef6\u8fdb\u884c\u8be6\u7ec6\u914d\u7f6e\u3002\u5176\u4ed6\u65b9\u6cd5\u5982cx_Freeze\u3001py2exe\u548cauto-py-to-exe\u4e5f\u5404\u6709\u7279\u70b9\uff0c\u9002\u5408\u4e0d\u540c\u7684\u9700\u6c42\u548c\u4f7f\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5c06Python\u4ee3\u7801\u8f6c\u6362\u4e3aexe\u683c\u5f0f\u7684\u4e3b\u8981\u6b65\u9aa4\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\u8981\u5c06Python\u4ee3\u7801\u8f6c\u6362\u4e3aexe\u683c\u5f0f\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u50cfPyInstaller\u3001cx_Freeze\u6216py2exe\u7b49\u5de5\u5177\u3002\u4ee5PyInstaller\u4e3a\u4f8b\uff0c\u60a8\u9700\u8981\u5b89\u88c5\u5b83\u5e76\u4f7f\u7528\u547d\u4ee4\u884c\u8fdb\u5165\u9879\u76ee\u76ee\u5f55\uff0c\u8fd0\u884c<code>pyinstaller your_script.py<\/code>\u547d\u4ee4\u3002\u8fd9\u6837\uff0c\u7a0b\u5e8f\u4f1a\u81ea\u52a8\u521b\u5efa\u4e00\u4e2a<code>dist<\/code>\u6587\u4ef6\u5939\uff0c\u5176\u4e2d\u5305\u542b\u53ef\u6267\u884c\u6587\u4ef6\u3002<\/p>\n<p><strong>\u8f6c\u6362\u540e\u751f\u6210\u7684exe\u6587\u4ef6\u53ef\u4ee5\u5728\u6ca1\u6709Python\u73af\u5883\u7684\u7535\u8111\u4e0a\u8fd0\u884c\u5417\uff1f<\/strong><br \/>\u662f\u7684\uff0c\u4f7f\u7528\u8fd9\u4e9b\u5de5\u5177\u751f\u6210\u7684exe\u6587\u4ef6\u662f\u72ec\u7acb\u7684\uff0c\u5141\u8bb8\u7528\u6237\u5728\u6ca1\u6709\u5b89\u88c5Python\u73af\u5883\u7684\u8ba1\u7b97\u673a\u4e0a\u8fd0\u884c\u3002\u8fd9\u4f7f\u5f97\u8f6f\u4ef6\u7684\u5206\u53d1\u53d8\u5f97\u66f4\u52a0\u7b80\u5355\uff0c\u56e0\u4e3a\u4e0d\u9700\u8981\u7528\u6237\u5b89\u88c5\u989d\u5916\u7684\u8fd0\u884c\u65f6\u73af\u5883\u3002<\/p>\n<p><strong>\u5728\u8f6c\u6362\u8fc7\u7a0b\u4e2d\u9047\u5230\u4f9d\u8d56\u95ee\u9898\u65f6\u8be5\u5982\u4f55\u89e3\u51b3\uff1f<\/strong><br \/>\u5982\u679c\u5728\u8f6c\u6362\u8fc7\u7a0b\u4e2d\u9047\u5230\u4f9d\u8d56\u95ee\u9898\uff0c\u53ef\u4ee5\u901a\u8fc7\u5728\u547d\u4ee4\u884c\u4e2d\u4f7f\u7528<code>--hidden-import<\/code>\u53c2\u6570\u6765\u624b\u52a8\u6307\u5b9a\u9700\u8981\u5305\u62ec\u7684\u6a21\u5757\u3002\u6b64\u5916\uff0c\u786e\u4fdd\u5728\u8fd0\u884c\u8f6c\u6362\u547d\u4ee4\u524d\uff0c\u6240\u6709\u4f9d\u8d56\u90fd\u5df2\u7ecf\u5b89\u88c5\u5e76\u5728\u4ee3\u7801\u4e2d\u6b63\u5e38\u5bfc\u5165\u3002\u67e5\u770bPyInstaller\u751f\u6210\u7684\u65e5\u5fd7\u6587\u4ef6\uff0c\u4e86\u89e3\u5177\u4f53\u7684\u9519\u8bef\u4fe1\u606f\u4e5f\u5f88\u6709\u5e2e\u52a9\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5c06Python\u4ee3\u7801\u8f6c\u5316\u4e3aexe\u683c\u5f0f\u7684\u65b9\u6cd5\u4e3b\u8981\u6709\u4ee5\u4e0b\u51e0\u79cd\uff1a\u4f7f\u7528PyInstaller\u3001\u4f7f\u7528cx_Freeze\u3001\u4f7f [&hellip;]","protected":false},"author":3,"featured_media":1095319,"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\/1095311"}],"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=1095311"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1095311\/revisions"}],"predecessor-version":[{"id":1095320,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1095311\/revisions\/1095320"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1095319"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1095311"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1095311"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1095311"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}