{"id":1095381,"date":"2025-01-08T14:50:32","date_gmt":"2025-01-08T06:50:32","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1095381.html"},"modified":"2025-01-08T14:50:35","modified_gmt":"2025-01-08T06:50:35","slug":"%e7%94%a8python%e5%a6%82%e4%bd%95%e8%bd%ac%e6%8d%a2%e6%88%90exe%e6%96%87%e4%bb%b6-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1095381.html","title":{"rendered":"\u7528python\u5982\u4f55\u8f6c\u6362\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\/24211053\/7f63edee-3ad3-4574-bf6e-4d7938c225c8.webp\" alt=\"\u7528python\u5982\u4f55\u8f6c\u6362\u6210exe\u6587\u4ef6\" \/><\/p>\n<p><p> <strong>\u8981\u5c06Python\u811a\u672c\u8f6c\u6362\u6210.exe\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u5de5\u5177\uff0c\u6bd4\u5982PyInstaller\u3001py2exe\u3001cx_Freeze\u7b49\u3002<\/strong> PyInstaller\u662f\u5176\u4e2d\u6700\u5e38\u7528\u548c\u6613\u4e8e\u4f7f\u7528\u7684\u5de5\u5177\u3002\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528PyInstaller\u5c06Python\u811a\u672c\u8f6c\u6362\u6210.exe\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5b89\u88c5PyInstaller<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u4f60\u9700\u8981\u5728\u4f60\u7684\u5f00\u53d1\u73af\u5883\u4e2d\u5b89\u88c5PyInstaller\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7pip\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">pip install pyinstaller<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7\u8fd0\u884c<code>pyinstaller --version<\/code>\u6765\u68c0\u67e5\u662f\u5426\u5b89\u88c5\u6210\u529f\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u57fa\u672c\u8f6c\u6362\u6b65\u9aa4<\/h3>\n<\/p>\n<ol>\n<li>\u6253\u5f00\u547d\u4ee4\u884c\u6216\u7ec8\u7aef\uff0c\u5bfc\u822a\u5230\u5305\u542bPython\u811a\u672c\u7684\u76ee\u5f55\u3002<\/li>\n<li>\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\u5c06\u811a\u672c\u8f6c\u6362\u4e3a.exe\u6587\u4ef6\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-sh\">pyinstaller --onefile your_script.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5176\u4e2d\uff0c<code>your_script.py<\/code>\u662f\u4f60\u60f3\u8981\u8f6c\u6362\u7684Python\u811a\u672c\u3002<code>--onefile<\/code>\u9009\u9879\u8868\u793a\u5c06\u6240\u6709\u4f9d\u8d56\u6253\u5305\u6210\u4e00\u4e2a\u5355\u72ec\u7684.exe\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u8be6\u7ec6\u914d\u7f6e\u4e0e\u4f18\u5316<\/h3>\n<\/p>\n<p><h4>1. \u6dfb\u52a0\u56fe\u6807<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u60f3\u4e3a\u4f60\u7684.exe\u6587\u4ef6\u6dfb\u52a0\u56fe\u6807\uff0c\u53ef\u4ee5\u4f7f\u7528<code>--icon<\/code>\u9009\u9879\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">pyinstaller --onefile --icon=your_icon.ico your_script.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u9690\u85cf\u63a7\u5236\u53f0\u7a97\u53e3<\/h4>\n<\/p>\n<p><p>\u5bf9\u4e8eGUI\u5e94\u7528\u7a0b\u5e8f\uff0c\u53ef\u80fd\u4e0d\u5e0c\u671b\u663e\u793a\u63a7\u5236\u53f0\u7a97\u53e3\uff0c\u53ef\u4ee5\u4f7f\u7528<code>--noconsole<\/code>\u9009\u9879\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">pyinstaller --onefile --noconsole your_script.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u52a0\u5bc6\u6e90\u4ee3\u7801<\/h4>\n<\/p>\n<p><p>\u4e3a\u4e86\u9632\u6b62\u4ee3\u7801\u88ab\u53cd\u7f16\u8bd1\uff0c\u53ef\u4ee5\u4f7f\u7528<code>--key<\/code>\u9009\u9879\u6765\u52a0\u5bc6Python\u5b57\u8282\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">pyinstaller --onefile --key=your_encryption_key your_script.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4. \u5305\u542b\u989d\u5916\u6570\u636e\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u7684\u811a\u672c\u4f9d\u8d56\u4e8e\u989d\u5916\u7684\u6570\u636e\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>--add-data<\/code>\u9009\u9879\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">pyinstaller --onefile --add-data &#39;data_file.txt;.&#39; your_script.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u751f\u6210\u7684\u6587\u4ef6\u7ed3\u6784<\/h3>\n<\/p>\n<p><p>\u6267\u884c\u5b8c\u4e0a\u8ff0\u547d\u4ee4\u540e\uff0c\u4f1a\u751f\u6210\u4e00\u4e2a\u540d\u4e3a<code>dist<\/code>\u7684\u76ee\u5f55\uff0c\u91cc\u9762\u5305\u542b\u751f\u6210\u7684.exe\u6587\u4ef6\u3002\u8fd8\u6709\u4e00\u4e2a<code>build<\/code>\u76ee\u5f55\u548c\u4e00\u4e2a<code>.spec<\/code>\u6587\u4ef6\u3002<code>build<\/code>\u76ee\u5f55\u5305\u542b\u4e2d\u95f4\u6587\u4ef6\uff0c<code>.spec<\/code>\u6587\u4ef6\u662fPyInstaller\u7684\u914d\u7f6e\u6587\u4ef6\uff0c\u53ef\u4ee5\u7528\u4e8e\u81ea\u5b9a\u4e49\u6253\u5305\u8fc7\u7a0b\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u5e38\u89c1\u95ee\u9898\u4e0e\u89e3\u51b3\u65b9\u6848<\/h3>\n<\/p>\n<p><h4>1. \u8fd0\u884c\u751f\u6210\u7684.exe\u6587\u4ef6\u65f6\u7f3a\u5c11DLL\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u6709\u65f6\u5019\u751f\u6210\u7684.exe\u6587\u4ef6\u5728\u8fd0\u884c\u65f6\u4f1a\u63d0\u793a\u7f3a\u5c11\u67d0\u4e9bDLL\u6587\u4ef6\u3002\u8fd9\u901a\u5e38\u662f\u56e0\u4e3a\u8fd9\u4e9bDLL\u6587\u4ef6\u6ca1\u6709\u88ab\u6b63\u786e\u5730\u6253\u5305\u8fdb\u53bb\u3002\u4f60\u53ef\u4ee5\u624b\u52a8\u5c06\u8fd9\u4e9bDLL\u6587\u4ef6\u590d\u5236\u5230.exe\u6587\u4ef6\u6240\u5728\u7684\u76ee\u5f55\uff0c\u6216\u8005\u5728.spec\u6587\u4ef6\u4e2d\u8fdb\u884c\u914d\u7f6e\u3002<\/p>\n<\/p>\n<p><h4>2. \u6587\u4ef6\u8def\u5f84\u95ee\u9898<\/h4>\n<\/p>\n<p><p>\u5728\u6253\u5305\u8fc7\u7a0b\u4e2d\uff0c\u6587\u4ef6\u8def\u5f84\u53ef\u80fd\u4f1a\u53d1\u751f\u53d8\u5316\u3002\u4e3a\u4e86\u786e\u4fdd\u7a0b\u5e8f\u80fd\u6b63\u786e\u627e\u5230\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>sys._MEIPASS<\/code>\u6765\u83b7\u53d6\u4e34\u65f6\u76ee\u5f55\u8def\u5f84\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<p>import os<\/p>\n<p>if getattr(sys, &#39;frozen&#39;, False):<\/p>\n<p>    base_path = sys._MEIPASS<\/p>\n<p>else:<\/p>\n<p>    base_path = os.path.abspath(&quot;.&quot;)<\/p>\n<p>file_path = os.path.join(base_path, &#39;your_file.txt&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u517c\u5bb9\u6027\u95ee\u9898<\/h4>\n<\/p>\n<p><p>\u786e\u4fdd\u4f60\u7684Python\u811a\u672c\u5728\u76ee\u6807\u5e73\u53f0\u4e0a\u6ca1\u6709\u517c\u5bb9\u6027\u95ee\u9898\u3002\u4e0d\u540c\u7248\u672c\u7684Python\u548c\u4e0d\u540c\u7684\u64cd\u4f5c\u7cfb\u7edf\u53ef\u80fd\u4f1a\u5bfc\u81f4\u517c\u5bb9\u6027\u95ee\u9898\u3002\u6700\u597d\u5728\u591a\u4e2a\u73af\u5883\u4e2d\u8fdb\u884c\u6d4b\u8bd5\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u4f7f\u7528.spec\u6587\u4ef6\u81ea\u5b9a\u4e49\u6253\u5305\u8fc7\u7a0b<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\u5019\uff0c\u7b80\u5355\u7684\u547d\u4ee4\u884c\u53c2\u6570\u53ef\u80fd\u65e0\u6cd5\u6ee1\u8db3\u590d\u6742\u7684\u9700\u6c42\u3002\u6b64\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528.spec\u6587\u4ef6\u6765\u81ea\u5b9a\u4e49\u6253\u5305\u8fc7\u7a0b\u3002\u751f\u6210.spec\u6587\u4ef6\u7684\u547d\u4ee4\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">pyinstaller --onefile your_script.py --name your_exe_name<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u751f\u6210\u7684.spec\u6587\u4ef6\u53ef\u4ee5\u624b\u52a8\u7f16\u8f91\uff0c\u6dfb\u52a0\u66f4\u591a\u81ea\u5b9a\u4e49\u9009\u9879\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684.spec\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;your_script.py&#39;],<\/p>\n<p>             pathex=[&#39;\/path\/to\/your\/script&#39;],<\/p>\n<p>             binaries=[],<\/p>\n<p>             datas=[(&#39;data_file.txt&#39;, &#39;.&#39;)],<\/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>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;your_exe_name&#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>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;your_exe_name&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728.spec\u6587\u4ef6\u4e2d\uff0c\u4f60\u53ef\u4ee5\u914d\u7f6e\u5404\u79cd\u6253\u5305\u9009\u9879\uff0c\u6bd4\u5982\u6dfb\u52a0\u6570\u636e\u6587\u4ef6\u3001\u9690\u85cf\u63a7\u5236\u53f0\u7a97\u53e3\u3001\u6307\u5b9a\u56fe\u6807\u7b49\u7b49\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5c06Python\u811a\u672c\u8f6c\u6362\u6210.exe\u6587\u4ef6\u662f\u4e00\u4e2a\u5e38\u89c1\u7684\u9700\u6c42\uff0c\u5c24\u5176\u662f\u5f53\u4f60\u5e0c\u671b\u5c06\u4f60\u7684Python\u5e94\u7528\u7a0b\u5e8f\u5206\u53d1\u7ed9\u4e0d\u5177\u5907Python\u73af\u5883\u7684\u7528\u6237\u65f6\u3002<strong>PyInstaller\u63d0\u4f9b\u4e86\u4e00\u4e2a\u7b80\u5355\u800c\u5f3a\u5927\u7684\u89e3\u51b3\u65b9\u6848<\/strong>\uff0c\u53ef\u4ee5\u6ee1\u8db3\u5927\u591a\u6570\u6253\u5305\u9700\u6c42\u3002\u901a\u8fc7\u5408\u7406\u914d\u7f6e\u547d\u4ee4\u884c\u53c2\u6570\u548c.spec\u6587\u4ef6\uff0c\u53ef\u4ee5\u751f\u6210\u4e00\u4e2a\u9ad8\u6548\u3001\u53ef\u9760\u7684.exe\u6587\u4ef6\u3002\u5e0c\u671b\u672c\u6587\u80fd\u5e2e\u52a9\u4f60\u987a\u5229\u5b8c\u6210Python\u811a\u672c\u5230.exe\u6587\u4ef6\u7684\u8f6c\u6362\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\uff08.exe\uff09\u901a\u5e38\u4f7f\u7528\u5de5\u5177\u5982PyInstaller\u6216cx_Freeze\u3002PyInstaller\u662f\u4e00\u4e2a\u5e7f\u6cdb\u4f7f\u7528\u7684\u5de5\u5177\uff0c\u5b83\u53ef\u4ee5\u5c06Python\u5e94\u7528\u7a0b\u5e8f\u6253\u5305\u6210\u72ec\u7acb\u7684\u53ef\u6267\u884c\u6587\u4ef6\uff0c\u652f\u6301Windows\u3001Linux\u548cMacOS\u3002\u53ea\u9700\u5b89\u88c5PyInstaller\uff0c\u4f7f\u7528\u547d\u4ee4\u884c\u8fd0\u884c<code>pyinstaller your_script.py<\/code>\uff0c\u5c31\u80fd\u5728dist\u6587\u4ef6\u5939\u4e2d\u627e\u5230\u751f\u6210\u7684.exe\u6587\u4ef6\u3002<\/p>\n<p><strong>\u8f6c\u6362\u4e3aexe\u6587\u4ef6\u540e\uff0c\u5982\u4f55\u786e\u4fdd\u7a0b\u5e8f\u5728\u5176\u4ed6\u8ba1\u7b97\u673a\u4e0a\u6b63\u5e38\u8fd0\u884c\uff1f<\/strong><br \/>\u5728\u5c06Python\u811a\u672c\u8f6c\u6362\u4e3a.exe\u6587\u4ef6\u540e\uff0c\u786e\u4fdd\u5176\u4ed6\u8ba1\u7b97\u673a\u4e0a\u53ef\u4ee5\u6b63\u5e38\u8fd0\u884c\u7684\u5173\u952e\u5728\u4e8e\u5305\u542b\u6240\u6709\u4f9d\u8d56\u5e93\u548c\u8d44\u6e90\u3002\u5728\u4f7f\u7528PyInstaller\u65f6\uff0c\u4f7f\u7528<code>--onefile<\/code>\u53c2\u6570\u53ef\u4ee5\u5c06\u6240\u6709\u4f9d\u8d56\u6587\u4ef6\u6253\u5305\u5230\u4e00\u4e2a\u53ef\u6267\u884c\u6587\u4ef6\u4e2d\uff0c\u907f\u514d\u7f3a\u5c11\u5e93\u7684\u95ee\u9898\u3002\u6b64\u5916\uff0c\u6d4b\u8bd5\u5728\u4e0d\u540c\u73af\u5883\u4e2d\u7684\u517c\u5bb9\u6027\u4e5f\u5f88\u91cd\u8981\uff0c\u786e\u4fdd\u6240\u6709\u5fc5\u8981\u7684\u7cfb\u7edf\u5e93\u548c\u8fd0\u884c\u65f6\u73af\u5883\u90fd\u5df2\u5b89\u88c5\u3002<\/p>\n<p><strong>\u5982\u679c\u8f6c\u6362\u540e\u51fa\u73b0\u9519\u8bef\uff0c\u5e94\u8be5\u5982\u4f55\u5904\u7406\uff1f<\/strong><br \/>\u5982\u679c\u5728\u5c06Python\u811a\u672c\u8f6c\u6362\u4e3a.exe\u6587\u4ef6\u540e\u9047\u5230\u9519\u8bef\uff0c\u9996\u5148\u68c0\u67e5\u9519\u8bef\u4fe1\u606f\uff0c\u901a\u5e38\u53ef\u4ee5\u63d0\u4f9b\u6709\u5173\u7f3a\u5c11\u4f9d\u8d56\u6216\u5176\u4ed6\u95ee\u9898\u7684\u7ebf\u7d22\u3002\u53ef\u4ee5\u5c1d\u8bd5\u4f7f\u7528<code>--debug<\/code>\u9009\u9879\u91cd\u65b0\u6253\u5305\uff0c\u4ee5\u83b7\u53d6\u8be6\u7ec6\u7684\u9519\u8bef\u65e5\u5fd7\u3002\u8fd9\u6709\u52a9\u4e8e\u8bc6\u522b\u95ee\u9898\u6240\u5728\u3002\u6b64\u5916\uff0c\u67e5\u770b\u6587\u6863\u548c\u793e\u533a\u8bba\u575b\uff0c\u5e38\u5e38\u80fd\u627e\u5230\u89e3\u51b3\u65b9\u6848\u6216\u5176\u4ed6\u7528\u6237\u9047\u5230\u76f8\u4f3c\u95ee\u9898\u7684\u7ecf\u9a8c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u5c06Python\u811a\u672c\u8f6c\u6362\u6210.exe\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u5de5\u5177\uff0c\u6bd4\u5982PyInstaller\u3001py2exe\u3001cx_Fr [&hellip;]","protected":false},"author":3,"featured_media":1095387,"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\/1095381"}],"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=1095381"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1095381\/revisions"}],"predecessor-version":[{"id":1095388,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1095381\/revisions\/1095388"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1095387"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1095381"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1095381"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1095381"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}