{"id":1110013,"date":"2025-01-08T17:15:42","date_gmt":"2025-01-08T09:15:42","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1110013.html"},"modified":"2025-01-08T17:15:45","modified_gmt":"2025-01-08T09:15:45","slug":"python%e5%a6%82%e4%bd%95%e8%bd%ac%e6%8d%a2%e6%88%90%e5%8f%af%e6%89%a7%e8%a1%8c%e7%a8%8b%e5%ba%8f-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1110013.html","title":{"rendered":"python\u5982\u4f55\u8f6c\u6362\u6210\u53ef\u6267\u884c\u7a0b\u5e8f"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25073033\/98271d0e-18c5-43ba-b810-01758967a0c6.webp\" alt=\"python\u5982\u4f55\u8f6c\u6362\u6210\u53ef\u6267\u884c\u7a0b\u5e8f\" \/><\/p>\n<p><p> <strong>Python\u53ef\u4ee5\u8f6c\u6362\u6210\u53ef\u6267\u884c\u7a0b\u5e8f\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528PyInstaller\u3001cx_Freeze\u3001py2exe\u7b49\u5de5\u5177\u3002<\/strong> \u5176\u4e2d\uff0c<strong>PyInstaller<\/strong>\u662f\u4e00\u79cd\u6700\u5e38\u7528\u4e14\u76f8\u5bf9\u7b80\u5355\u7684\u65b9\u6cd5\u3002\u4e0b\u9762\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528PyInstaller\u5c06Python\u811a\u672c\u8f6c\u6362\u6210\u53ef\u6267\u884c\u7a0b\u5e8f\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5b89\u88c5PyInstaller<\/h3>\n<\/p>\n<p><h4>\u5b89\u88c5\u65b9\u6cd5<\/h4>\n<\/p>\n<p><p>\u8981\u5c06Python\u811a\u672c\u8f6c\u6362\u4e3a\u53ef\u6267\u884c\u6587\u4ef6\uff0c\u9996\u5148\u9700\u8981\u5b89\u88c5PyInstaller\u3002\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\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><h4>\u68c0\u67e5\u5b89\u88c5<\/h4>\n<\/p>\n<p><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u6765\u68c0\u67e5\u662f\u5426\u5b89\u88c5\u6210\u529f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pyinstaller --version<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5982\u679c\u663e\u793a\u7248\u672c\u53f7\uff0c\u8bf4\u660e\u5b89\u88c5\u6210\u529f\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528PyInstaller\u5c06Python\u811a\u672c\u8f6c\u6362\u4e3a\u53ef\u6267\u884c\u6587\u4ef6<\/h3>\n<\/p>\n<p><h4>\u57fa\u672c\u7528\u6cd5<\/h4>\n<\/p>\n<p><p>\u8f6c\u6362Python\u811a\u672c\u53ea\u9700\u8981\u4e00\u4e2a\u7b80\u5355\u7684\u547d\u4ee4\u3002\u5047\u8bbe\u4f60\u7684\u811a\u672c\u540d\u4e3a<code>script.py<\/code>\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pyinstaller script.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>\u76ee\u5f55\uff0c\u5176\u4e2d\u5305\u542b\u751f\u6210\u7684\u53ef\u6267\u884c\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h4>\u8be6\u7ec6\u53c2\u6570<\/h4>\n<\/p>\n<p><p>PyInstaller\u63d0\u4f9b\u4e86\u5f88\u591a\u53c2\u6570\u6765\u5b9a\u5236\u751f\u6210\u7684\u53ef\u6267\u884c\u6587\u4ef6\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u7528\u7684\u53c2\u6570\uff1a<\/p>\n<\/p>\n<ul>\n<li><code>--onefile<\/code>\uff1a\u5c06\u6240\u6709\u4f9d\u8d56\u6253\u5305\u6210\u4e00\u4e2a\u6587\u4ef6\u3002<\/li>\n<li><code>--noconsole<\/code>\uff1a\u751f\u6210\u7684\u7a0b\u5e8f\u8fd0\u884c\u65f6\u4e0d\u663e\u793a\u63a7\u5236\u53f0\u7a97\u53e3\uff08\u9002\u7528\u4e8eGUI\u5e94\u7528\uff09\u3002<\/li>\n<li><code>--icon=icon.ico<\/code>\uff1a\u4e3a\u751f\u6210\u7684\u53ef\u6267\u884c\u6587\u4ef6\u6307\u5b9a\u56fe\u6807\u3002<\/li>\n<\/ul>\n<p><p>\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u60f3\u5c06\u6240\u6709\u4f9d\u8d56\u6253\u5305\u6210\u4e00\u4e2a\u6587\u4ef6\uff0c\u5e76\u9690\u85cf\u63a7\u5236\u53f0\u7a97\u53e3\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pyinstaller --onefile --noconsole script.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u914d\u7f6e.spec\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u5f53\u4f60\u8fd0\u884c<code>pyinstaller<\/code>\u547d\u4ee4\u65f6\uff0c\u5b83\u4f1a\u5728\u5f53\u524d\u76ee\u5f55\u751f\u6210\u4e00\u4e2a<code>.spec<\/code>\u6587\u4ef6\u3002\u8fd9\u4e2a\u6587\u4ef6\u5305\u542b\u4e86\u6253\u5305\u8fc7\u7a0b\u4e2d\u7684\u6240\u6709\u914d\u7f6e\u4fe1\u606f\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u7f16\u8f91\u8fd9\u4e2a\u6587\u4ef6\u6765\u8fdb\u4e00\u6b65\u5b9a\u5236\u6253\u5305\u8fc7\u7a0b\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b.spec\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684<code>.spec<\/code>\u6587\u4ef6\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># -*- mode: python ; coding: utf-8 -*-<\/p>\n<p>block_cipher = None<\/p>\n<p>a = Analysis([&#39;script.py&#39;],<\/p>\n<p>             pathex=[&#39;\/path\/to\/your\/script&#39;],<\/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>             noarchive=False)<\/p>\n<p>pyz = PYZ(a.pure, a.zipped_data, cipher=block_cipher)<\/p>\n<p>exe = EXE(pyz,<\/p>\n<p>          a.scripts,<\/p>\n<p>          [],<\/p>\n<p>          exclude_binaries=True,<\/p>\n<p>          name=&#39;script&#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>          console=True,<\/p>\n<p>          icon=&#39;icon.ico&#39;)<\/p>\n<p>coll = COLLECT(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;script&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u4fee\u6539.spec\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u6839\u636e\u9700\u8981\u4fee\u6539<code>.spec<\/code>\u6587\u4ef6\uff0c\u4f8b\u5982\u6dfb\u52a0\u6570\u636e\u6587\u4ef6\u3001\u4fee\u6539\u56fe\u6807\u7b49\u3002\u4fee\u6539\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u4f7f\u7528<code>.spec<\/code>\u6587\u4ef6\u6765\u751f\u6210\u53ef\u6267\u884c\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pyinstaller script.spec<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u5176\u4ed6\u5de5\u5177<\/h3>\n<\/p>\n<p><h4>cx_Freeze<\/h4>\n<\/p>\n<p><p>\u53e6\u4e00\u4e2a\u5e38\u7528\u7684\u5de5\u5177\u662fcx_Freeze\u3002\u5b83\u4e5f\u662f\u4e00\u4e2a\u53ef\u4ee5\u5c06Python\u811a\u672c\u8f6c\u6362\u6210\u53ef\u6267\u884c\u6587\u4ef6\u7684\u5de5\u5177\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528cx_Freeze\u7684\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u5b89\u88c5cx_Freeze\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>\u521b\u5efa\u4e00\u4e2a<code>setup.py<\/code>\u6587\u4ef6\uff0c\u5185\u5bb9\u5982\u4e0b\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">from cx_Freeze import setup, Executable<\/p>\n<p>setup(<\/p>\n<p>    name = &quot;script&quot;,<\/p>\n<p>    version = &quot;0.1&quot;,<\/p>\n<p>    description = &quot;Your Description&quot;,<\/p>\n<p>    executables = [Executable(&quot;script.py&quot;)]<\/p>\n<p>)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li>\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\u751f\u6210\u53ef\u6267\u884c\u6587\u4ef6\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-bash\">python setup.py build<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>py2exe<\/h4>\n<\/p>\n<p><p>py2exe\u662f\u53e6\u4e00\u4e2a\u5c06Python\u811a\u672c\u8f6c\u6362\u4e3aWindows\u53ef\u6267\u884c\u6587\u4ef6\u7684\u5de5\u5177\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528py2exe\u7684\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u5b89\u88c5py2exe\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>\u521b\u5efa\u4e00\u4e2a<code>setup.py<\/code>\u6587\u4ef6\uff0c\u5185\u5bb9\u5982\u4e0b\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">from distutils.core import setup<\/p>\n<p>import py2exe<\/p>\n<p>setup(console=[&#39;script.py&#39;])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li>\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\u751f\u6210\u53ef\u6267\u884c\u6587\u4ef6\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-bash\">python setup.py py2exe<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p><strong>\u5c06Python\u811a\u672c\u8f6c\u6362\u6210\u53ef\u6267\u884c\u7a0b\u5e8f\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff0c\u6700\u5e38\u7528\u7684\u662fPyInstaller\uff0c\u5176\u6b21\u662fcx_Freeze\u548cpy2exe\u3002<\/strong> \u4f7f\u7528\u8fd9\u4e9b\u5de5\u5177\u53ef\u4ee5\u65b9\u4fbf\u5730\u5c06\u4f60\u7684Python\u811a\u672c\u6253\u5305\u6210\u72ec\u7acb\u7684\u53ef\u6267\u884c\u6587\u4ef6\uff0c\u65b9\u4fbf\u5206\u53d1\u548c\u90e8\u7f72\u3002\u901a\u8fc7\u7406\u89e3\u548c\u914d\u7f6e\u8fd9\u4e9b\u5de5\u5177\u7684\u5404\u79cd\u53c2\u6570\u548c\u914d\u7f6e\u6587\u4ef6\uff0c\u53ef\u4ee5\u6ee1\u8db3\u4e0d\u540c\u7684\u6253\u5305\u9700\u6c42\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5c06Python\u811a\u672c\u6253\u5305\u6210\u53ef\u6267\u884c\u6587\u4ef6\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528\u50cfPyInstaller\u3001cx_Freeze\u6216py2exe\u8fd9\u6837\u7684\u5de5\u5177\u6765\u5c06Python\u811a\u672c\u6253\u5305\u6210\u53ef\u6267\u884c\u6587\u4ef6\u3002\u8fd9\u4e9b\u5de5\u5177\u4f1a\u5c06\u4f60\u7684Python\u4ee3\u7801\u53ca\u5176\u4f9d\u8d56\u9879\u6253\u5305\u6210\u4e00\u4e2a\u72ec\u7acb\u7684\u53ef\u6267\u884c\u6587\u4ef6\uff0c\u7528\u6237\u65e0\u9700\u5b89\u88c5Python\u73af\u5883\u5373\u53ef\u8fd0\u884c\u3002\u4f7f\u7528PyInstaller\u65f6\uff0c\u53ea\u9700\u5728\u547d\u4ee4\u884c\u4e2d\u8f93\u5165<code>pyinstaller your_script.py<\/code>\uff0c\u4fbf\u4f1a\u751f\u6210\u4e00\u4e2a\u53ef\u6267\u884c\u6587\u4ef6\u3002<\/p>\n<p><strong>\u5728\u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\u4e2d\u8f6c\u6362\u4e3a\u53ef\u6267\u884c\u7a0b\u5e8f\u6709\u4ec0\u4e48\u533a\u522b\u5417\uff1f<\/strong><br \/>\u662f\u7684\uff0c\u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\u5728\u6253\u5305\u8fc7\u7a0b\u4e2d\u4f1a\u6709\u4e00\u4e9b\u5dee\u5f02\u3002Windows\u901a\u5e38\u4f7f\u7528PyInstaller\u6216py2exe\uff0c\u800cLinux\u7528\u6237\u53ef\u4ee5\u4f7f\u7528PyInstaller\u6216cx_Freeze\u3002macOS\u7528\u6237\u540c\u6837\u53ef\u4ee5\u4f7f\u7528PyInstaller\u3002\u6bcf\u79cd\u5de5\u5177\u5bf9\u7279\u5b9a\u64cd\u4f5c\u7cfb\u7edf\u7684\u652f\u6301\u548c\u914d\u7f6e\u53ef\u80fd\u6709\u6240\u4e0d\u540c\uff0c\u56e0\u6b64\u5728\u6253\u5305\u524d\u5e94\u4ed4\u7ec6\u9605\u8bfb\u76f8\u5173\u6587\u6863\u3002<\/p>\n<p><strong>\u8f6c\u6362\u540e\u7684\u53ef\u6267\u884c\u7a0b\u5e8f\u5728\u8fd0\u884c\u65f6\u4f1a\u9047\u5230\u54ea\u4e9b\u5e38\u89c1\u95ee\u9898\uff1f<\/strong><br \/>\u5e38\u89c1\u95ee\u9898\u5305\u62ec\u7f3a\u5c11\u4f9d\u8d56\u5e93\u3001\u8def\u5f84\u95ee\u9898\u548c\u6743\u9650\u95ee\u9898\u3002\u5982\u679c\u7a0b\u5e8f\u8fd0\u884c\u65f6\u63d0\u793a\u627e\u4e0d\u5230\u6a21\u5757\uff0c\u53ef\u80fd\u662f\u56e0\u4e3a\u67d0\u4e9b\u4f9d\u8d56\u672a\u88ab\u6b63\u786e\u6253\u5305\u3002\u786e\u4fdd\u5728\u6253\u5305\u8fc7\u7a0b\u4e2d\u5305\u542b\u6240\u6709\u5fc5\u9700\u7684\u6587\u4ef6\u548c\u5e93\u3002\u6b64\u5916\uff0c\u8def\u5f84\u95ee\u9898\u53ef\u80fd\u4f1a\u5f71\u54cd\u6587\u4ef6\u7684\u8bfb\u53d6\uff0c\u786e\u4fdd\u4f7f\u7528\u76f8\u5bf9\u8def\u5f84\u800c\u975e\u7edd\u5bf9\u8def\u5f84\u6765\u907f\u514d\u6b64\u7c7b\u95ee\u9898\u3002\u7528\u6237\u6743\u9650\u4e0d\u8db3\u65f6\uff0c\u53ef\u80fd\u5bfc\u81f4\u7a0b\u5e8f\u65e0\u6cd5\u6267\u884c\uff0c\u786e\u4fdd\u6709\u9002\u5f53\u7684\u6743\u9650\u8bbe\u7f6e\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u53ef\u4ee5\u8f6c\u6362\u6210\u53ef\u6267\u884c\u7a0b\u5e8f\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528PyInstaller\u3001cx_Freeze\u3001py2exe [&hellip;]","protected":false},"author":3,"featured_media":1110021,"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\/1110013"}],"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=1110013"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1110013\/revisions"}],"predecessor-version":[{"id":1110023,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1110013\/revisions\/1110023"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1110021"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1110013"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1110013"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1110013"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}