{"id":1627,"date":"2025-12-23T14:52:12","date_gmt":"2025-12-23T06:52:12","guid":{"rendered":"https:\/\/swordofmorning.com\/?p=1627"},"modified":"2025-12-23T14:52:12","modified_gmt":"2025-12-23T06:52:12","slug":"pyinstaller","status":"publish","type":"post","link":"https:\/\/swordofmorning.com\/index.php\/2025\/12\/23\/pyinstaller\/","title":{"rendered":"Pyinstaller \u5165\u95e8\u7b80\u4ecb"},"content":{"rendered":"<p>&emsp;&emsp;\u672c\u7bc7\u535a\u5ba2\u5c06\u7b80\u8981\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528<code>pyinstaller<\/code>\u6784\u5efa\u3001\u6253\u5305\u4e00\u4e2apython\u7a0b\u5e8f\u3002\u8fd9\u91cc\u6211\u4eec\u5c06\u4ee5C\u98ce\u683c\u7684\u65b9\u5f0f(<code>.spec<\/code>\u914d\u7f6e\u6587\u4ef6 + <code>build<\/code>\u811a\u672c)\u6765\u6253\u5305\u6211\u4eec\u7684\u76ee\u6807\u6e90\u7801\u3002\u56e0\u6b64\u6211\u4eec\u9700\u8981\u51c6\u5907\u5982\u4e0b\u7684\u5185\u5bb9\uff1a<\/p>\n<ol>\n<li><code>main.py<\/code>\uff0c\u76ee\u6807\u6e90\u7801\uff1b<\/li>\n<li><code>build_config.spec<\/code>\uff0c\u6784\u5efa\u914d\u7f6e\uff1b<\/li>\n<li><code>build.py<\/code>\uff0c\u6784\u5efa\u811a\u672c\u3002<\/li>\n<\/ol>\n<h2>\u4e00\u3001main.py<\/h2>\n<p>&emsp;&emsp;\u8fd9\u91cc\u6211\u4eec\u5c06\u5b9e\u73b0\u4e00\u4e2a\u6700\u57fa\u672c\u7684python\u7a0b\u5e8f\uff0c\u6211\u4eec\u671f\u671b\u901a\u8fc7\u5982\u4e0b\u7684\u65b9\u5f0f\u6765\u8c03\u7528\u5b83\uff1a<\/p>\n<pre><code class=\"language-log\">(base)PS C:\\Users\\x\\Downloads\\src&gt; python main.py --print &quot;Hello&quot;\n[INFO] \u63a5\u6536\u5230\u7684\u53c2\u6570\u5185\u5bb9:  Hello<\/code><\/pre>\n<p>&emsp;&emsp;\u5176\u6e90\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-py\"># main.py\n\n# \u6807\u51c6\u5e93\nimport argparse\nimport sys\n# \u7b2c\u4e09\u65b9\u5e93\nfrom colorama import init, Fore, Style\n\n# \u521d\u59cb\u5316 colorama\ninit()\n\ndef main():\n    # 1. \u521b\u5efa ArgumentParser \u5bf9\u8c61\n    parser = argparse.ArgumentParser(description=&quot;\u4e00\u4e2a\u7528\u4e8e\u6f14\u793a PyInstaller \u6253\u5305\u7684\u793a\u4f8b\u7a0b\u5e8f&quot;)\n\n    # 2. \u6dfb\u52a0\u53c2\u6570 --print\n    parser.add_argument(&#039;--print&#039;, type=str, help=&#039;\u9700\u8981\u6253\u5370\u7684\u5b57\u7b26\u4e32\u5185\u5bb9&#039;)\n\n    # 3. \u89e3\u6790\u53c2\u6570\n    args = parser.parse_args()\n\n    # 4. \u4e1a\u52a1\u903b\u8f91\n    if args.print:\n        # \u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93 colorama \u8f93\u51fa\u7eff\u8272\u6587\u5b57\n        print(f&quot;{Fore.GREEN}[INFO] \u63a5\u6536\u5230\u7684\u53c2\u6570\u5185\u5bb9: {Style.RESET_ALL} {args.print}&quot;)\n    else:\n        print(f&quot;{Fore.RED}[ERROR] \u672a\u63d0\u4f9b --print \u53c2\u6570\u3002{Style.RESET_ALL}&quot;)\n        print(f&quot;\u7528\u6cd5\u793a\u4f8b: {sys.argv[0]} --print \\&quot;Hello World\\&quot;&quot;)\n\nif __name__ == &quot;__main__&quot;:\n    main()<\/code><\/pre>\n<p>\u8fd9\u91cc\u6211\u4eec\u7f16\u5199\u4e86\u4e00\u4e2a\u6700\u57fa\u672c\u7684IO\u7a0b\u5e8f\uff0c\u540c\u65f6\u52a0\u5165\u4e86\u7b2c\u4e09\u65b9\u5e93<code>colorama<\/code>\uff0c\u8ba9\u7a0b\u5e8f\u66f4\u8d34\u8fd1\u5b9e\u9645\u4f7f\u7528\u7684\u60c5\u51b5\u3002<\/p>\n<h2>\u4e8c\u3001build_config.spec<\/h2>\n<p>&emsp;&emsp;\u5728\u9a8c\u8bc1\u4e86\u4e0a\u8ff0\u4ee3\u7801\u7684\u6b63\u786e\u6027\u540e\uff0c\u6211\u4eec\u5c1d\u8bd5\u4f7f\u7528<code>pyinstaller<\/code>\u5bf9\u5176\u8fdb\u884c\u6253\u5305\u3002\u4e3a\u4e86\u66f4\u597d\u7684\u63a7\u5236\u7f16\u8bd1\u8fc7\u7a0b\uff0c\u6211\u4eec\u8fd9\u91cc\u4e0d\u76f4\u63a5\u91c7\u7528<code>pyinstaller xxxxxx<\/code>\u7684\u65b9\u5f0f\u6765\u7f16\u8bd1\uff0c\u800c\u662f\u50cf<code>makefile<\/code>\u4e00\u6837\u7f16\u5199\u4e00\u4e2a\u4e13\u95e8\u7684\u6587\u4ef6\u3002\u5176\u6e90\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-py\"># demo_app.spec\n# -*- mode: python ; coding: utf-8 -*-\n\nblock_cipher = None\n\na = Analysis(\n    [&#039;main.py&#039;],             # \u6e90\u4ee3\u7801\u5217\u8868\n    pathex=[],               # \u989d\u5916\u7684\u641c\u7d22\u8def\u5f84\n    binaries=[],             # \u9700\u8981\u5305\u542b\u7684\u4e8c\u8fdb\u5236\u6587\u4ef6 (\u5982 .dll, .so)\n    datas=[],                # \u975e\u4ee3\u7801\u8d44\u6e90\u6587\u4ef6 (\u5982\u56fe\u7247, \u914d\u7f6e\u6587\u4ef6)\n    hiddenimports=[],        # PyInstaller \u65e0\u6cd5\u81ea\u52a8\u68c0\u6d4b\u5230\u7684\u9690\u5f0f\u5bfc\u5165\u5e93\n    hookspath=[],\n    hooksconfig={},\n    runtime_hooks=[],\n    excludes=[],             # \u4e0d\u9700\u8981\u6253\u5305\u7684\u5e93\n    win_no_prefer_redirects=False,\n    win_private_assemblies=False,\n    cipher=block_cipher,\n    noarchive=False,\n)\n\npyz = PYZ(a.pure, a.zipped_data, cipher=block_cipher)\n\nexe = EXE(\n    pyz,\n    a.scripts,\n    a.binaries,\n    a.zipfiles,\n    a.datas,\n    [],\n    name=&#039;demo_app&#039;,          # \u8f93\u51fa\u7684\u53ef\u6267\u884c\u6587\u4ef6\u540d\n    debug=False,\n    bootloader_ignore_signals=False,\n    strip=False,\n    upx=True,                 # \u662f\u5426\u4f7f\u7528 UPX \u538b\u7f29\n    upx_exclude=[],\n    runtime_tmpdir=None,\n    console=True,             # True=\u663e\u793a\u63a7\u5236\u53f0(\u547d\u4ee4\u884c\u7a0b\u5e8f), False=\u9690\u85cf(GUI\u7a0b\u5e8f)\n    disable_windowed_traceback=False,\n    argv_emulation=False,\n    target_arch=None,\n    codesign_identity=None,\n    entitlements_file=None,\n)<\/code><\/pre>\n<h2>\u4e09\u3001build.py<\/h2>\n<p>&emsp;&emsp;\u5728\u7f16\u5199\u5b8c\u4e86\u4e0a\u8ff0\u914d\u7f6e\u6587\u4ef6\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u7528\u7c7b\u4f3c<code>make xxxx.conf<\/code>\u7684\u65b9\u5f0f\u6765\u8c03\u7528\u5b83\uff1b\u540c\u6837\u7684\uff0c\u6211\u4eec\u8fd9\u91cc\u4f9d\u7136\u91c7\u7528\u7f16\u5199\u4e00\u4e2a\u811a\u672c(bash\u3001powershell\u5747\u53ef)\u6765\u63a7\u5236\u6784\u5efa\u7684\u6d41\u7a0b\uff0c\u4e3a\u4e86\u65b9\u4fbf\uff0c\u6211\u4eec\u8fd9\u91cc\u4f9d\u7136\u9009\u62e9\u4f7f\u7528python\u6765\u4f5c\u4e3a\u6784\u5efa\u811a\u672c\u3002\u5176\u6e90\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-py\"># build.py\nimport PyInstaller.__main__\nimport os\nimport shutil\n\ndef build():\n    print(&quot;\u4f7f\u7528 Spec \u6587\u4ef6\u5f00\u59cb\u6784\u5efa...&quot;)\n\n    # 1. clean\u539f\u6709\u6784\u5efa\u6587\u4ef6\n    if os.path.exists(&#039;dist&#039;):\n        shutil.rmtree(&#039;dist&#039;)\n    if os.path.exists(&#039;build&#039;):\n        shutil.rmtree(&#039;build&#039;)\n\n    # 2. \u9009\u62e9\u914d\u7f6e\u6587\u4ef6 build_config.spec\n    params = [\n        &#039;build_config.spec&#039;,\n        # \u6784\u5efa\u524d\u6e05\u7406\u6587\u4ef6\n        &#039;--clean&#039;\n    ]\n\n    # 3. (Make) \u8c03\u7528pyinstaller\u6267\u884c\u6784\u5efa\n    PyInstaller.__main__.run(params)\n\n    print(&quot;\u6784\u5efa\u5b8c\u6210\uff01&quot;)\n\nif __name__ == &quot;__main__&quot;:\n    build()<\/code><\/pre>\n<h2>\u56db\u3001\u9a8c\u8bc1<\/h2>\n<p>&emsp;&emsp;\u5728\u7f16\u5199\u5b8c\u6210\u4e86\u4e0a\u8ff0\u4ee3\u7801\u4e4b\u540e\uff0c\u6211\u4eec\u7684\u5de5\u7a0b\u76ee\u5f55\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-log\">.\n\u251c\u2500\u2500 build_config.spec\n\u251c\u2500\u2500 build.py\n\u2514\u2500\u2500 main.py<\/code><\/pre>\n<p>&emsp;&emsp;\u6211\u4eec\u9996\u5148\u8fdb\u884c<code>main.py<\/code>\u7684\u529f\u80fd\u9a8c\u8bc1\uff1a<\/p>\n<pre><code class=\"language-log\">(base)PS C:\\Users\\x\\Downloads\\src&gt; python main.py --print &quot;\u529f\u80fd\u9a8c\u8bc1&quot;\n[INFO] \u63a5\u6536\u5230\u7684\u53c2\u6570\u5185\u5bb9:  \u529f\u80fd\u9a8c\u8bc1<\/code><\/pre>\n<p>\u786e\u8ba4\u6ca1\u6709\u95ee\u9898\u4e4b\u540e\uff0c\u901a\u8fc7<code>build.py<\/code>\u8fdb\u884c\u7f16\u8bd1\uff1a<\/p>\n<pre><code class=\"language-log\">(base)PS C:\\Users\\x\\Downloads\\src&gt; python build.py\n\u4f7f\u7528 Spec \u6587\u4ef6\u5f00\u59cb\u6784\u5efa...\n579 INFO: PyInstaller: 6.11.1, contrib hooks: 2025.0\n580 INFO: Python: 3.12.4 (conda)\n602 INFO: Platform: Windows-11-10.0.26100-SP0\n602 INFO: Python environment: C:\\Users\\x\\miniconda3\n609 INFO: Removing temporary files and cleaning cache in C:\\Users\\x\\AppData\\Local\\pyinstaller\n611 INFO: Module search paths (PYTHONPATH):\n[&#039;C:\\\\Users\\\\x\\\\Downloads\\\\src&#039;,\n &#039;C:\\\\Users\\\\x\\\\miniconda3\\\\python312.zip&#039;,\n &#039;C:\\\\Users\\\\x\\\\miniconda3\\\\DLLs&#039;,\n &#039;C:\\\\Users\\\\x\\\\miniconda3\\\\Lib&#039;,\n &#039;C:\\\\Users\\\\x\\\\miniconda3&#039;,\n &#039;C:\\\\Users\\\\x\\\\miniconda3\\\\Lib\\\\site-packages&#039;,\n &#039;C:\\\\Users\\\\x\\\\miniconda3\\\\Lib\\\\site-packages\\\\win32&#039;,\n &#039;C:\\\\Users\\\\x\\\\miniconda3\\\\Lib\\\\site-packages\\\\win32\\\\lib&#039;,\n &#039;C:\\\\Users\\\\x\\\\miniconda3\\\\Lib\\\\site-packages\\\\Pythonwin&#039;,\n &#039;C:\\\\Users\\\\x\\\\Downloads\\\\src&#039;]\n1367 INFO: checking Analysis\n1367 INFO: Building Analysis because Analysis-00.toc is non existent\n1367 INFO: Running Analysis Analysis-00.toc\n1367 INFO: Target bytecode optimization level: 0\n1369 INFO: Initializing module dependency graph...\n1370 INFO: Initializing module graph hook caches...\n1398 INFO: Analyzing base_library.zip ...\n3690 INFO: Processing standard module hook &#039;hook-encodings.py&#039; from &#039;C:\\\\Users\\\\x\\\\miniconda3\\\\Lib\\\\site-packages\\\\PyInstaller\\\\hooks&#039;\n5436 INFO: Processing standard module hook &#039;hook-pickle.py&#039; from &#039;C:\\\\Users\\\\x\\\\miniconda3\\\\Lib\\\\site-packages\\\\PyInstaller\\\\hooks&#039;\n6307 INFO: Processing standard module hook &#039;hook-heapq.py&#039; from &#039;C:\\\\Users\\\\x\\\\miniconda3\\\\Lib\\\\site-packages\\\\PyInstaller\\\\hooks&#039;\n6755 INFO: Caching module dependency graph...\n6865 INFO: Looking for Python shared library...\n6872 INFO: Using Python shared library: C:\\Users\\x\\miniconda3\\python312.dll\n6872 INFO: Analyzing C:\\Users\\x\\Downloads\\src\\main.py\n6934 INFO: Processing module hooks (post-graph stage)...\n6938 INFO: Performing binary vs. data reclassification (2 entries)\n6941 INFO: Looking for ctypes DLLs\n6947 INFO: Analyzing run-time hooks ...\n6949 INFO: Including run-time hook &#039;pyi_rth_inspect.py&#039; from &#039;C:\\\\Users\\\\x\\\\miniconda3\\\\Lib\\\\site-packages\\\\PyInstaller\\\\hooks\\\\rthooks&#039;\n6953 INFO: Looking for dynamic libraries\n7150 INFO: Extra DLL search directories (AddDllDirectory): []\n7151 INFO: Extra DLL search directories (PATH): []\n7702 INFO: Warnings written to C:\\Users\\x\\Downloads\\src\\build\\build_config\\warn-build_config.txt\n7725 INFO: Graph cross-reference written to C:\\Users\\x\\Downloads\\src\\build\\build_config\\xref-build_config.html\n7762 INFO: checking PYZ\n7762 INFO: Building PYZ because PYZ-00.toc is non existent\n7762 INFO: Building PYZ (ZlibArchive) C:\\Users\\x\\Downloads\\src\\build\\build_config\\PYZ-00.pyz\n8015 INFO: Building PYZ (ZlibArchive) C:\\Users\\x\\Downloads\\src\\build\\build_config\\PYZ-00.pyz completed successfully.\n8029 INFO: checking PKG\n8029 INFO: Building PKG because PKG-00.toc is non existent\n8029 INFO: Building PKG (CArchive) demo_app.pkg\n9912 INFO: Building PKG (CArchive) demo_app.pkg completed successfully.\n9914 INFO: Bootloader C:\\Users\\x\\miniconda3\\Lib\\site-packages\\PyInstaller\\bootloader\\Windows-64bit-intel\\run.exe\n9914 INFO: checking EXE\n9915 INFO: Building EXE because EXE-00.toc is non existent\n9915 INFO: Building EXE from EXE-00.toc\n9915 INFO: Copying bootloader EXE to C:\\Users\\x\\Downloads\\src\\dist\\demo_app.exe\n9925 INFO: Copying icon to EXE\n9933 INFO: Copying 0 resources to EXE\n9934 INFO: Embedding manifest in EXE\n9941 INFO: Appending PKG archive to EXE\n9957 INFO: Fixing EXE headers\n10044 INFO: Building EXE from EXE-00.toc completed successfully.\n\u6784\u5efa\u5b8c\u6210\uff01\n(base)PS C:\\Users\\x\\Downloads\\src&gt;<\/code><\/pre>\n<p>\u6211\u4eec\u53ef\u4ee5\u770b\u5230\uff0c\u5df2\u7ecf\u6210\u529f\u7f16\u8bd1\u4e86<code>main.py<\/code>\uff0c\u8f93\u51fa\u7684\u7ed3\u679c\u4e3a<code>dist\/demo_app.exe<\/code>\uff0c\u6211\u4eec\u5c1d\u8bd5\u8c03\u7528\u5b83\uff1a<\/p>\n<pre><code class=\"language-log\">(base)PS C:\\Users\\x\\Downloads\\src&gt; .\\dist\\demo_app.exe --print &quot;\u9a8c\u8bc1\u6784\u5efa\u4ea7\u7269&quot;\n[INFO] \u63a5\u6536\u5230\u7684\u53c2\u6570\u5185\u5bb9:  \u9a8c\u8bc1\u6784\u5efa\u4ea7\u7269<\/code><\/pre>\n<p>\u81f3\u6b64\u6211\u4eec\u8fd9\u91cc\u5b8c\u6210\u4e86\u901a\u8fc7<code>pyinstaller<\/code>\u8fdb\u884cpython\u7a0b\u5e8f\u6253\u5305\u7684\u8fd9\u4e00\u76ee\u7684\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>&emsp;&emsp;\u672c\u7bc7\u535a\u5ba2\u5c06\u7b80\u8981\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528pyinstaller\u6784\u5efa\u3001\u6253\u5305\u4e00\u4e2apython\u7a0b\u5e8f\u3002\u8fd9\u91cc\u6211\u4eec\u5c06\u4ee5C\u98ce\u683c\u7684\u65b9\u5f0f( &#8230;<\/p>","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[57],"tags":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/posts\/1627"}],"collection":[{"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/comments?post=1627"}],"version-history":[{"count":1,"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/posts\/1627\/revisions"}],"predecessor-version":[{"id":1628,"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/posts\/1627\/revisions\/1628"}],"wp:attachment":[{"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/media?parent=1627"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/categories?post=1627"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/tags?post=1627"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}