{"id":19945,"date":"2025-05-04T13:55:17","date_gmt":"2025-05-04T05:55:17","guid":{"rendered":"https:\/\/blog.iyatt.com\/?p=19945"},"modified":"2026-09-05T23:39:16","modified_gmt":"2026-09-05T15:39:16","slug":"windows-%e4%b8%8b-nuitka-%e6%98%af%e5%90%a6%e6%89%93%e5%8c%85%e7%8a%b6%e6%80%81%e5%88%a4%e6%96%ad%e5%8f%8a%e8%b7%af%e5%be%84%e8%8e%b7%e5%8f%96","status":"publish","type":"post","link":"https:\/\/blog.iyatt.com\/?p=19945","title":{"rendered":"Windows \u4e0b Nuitka \u6253\u5305 Python \u72b6\u6001\u53ca\u8def\u5f84\u7814\u7a76"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_89 ez-toc-wrap-center counter-hierarchy ez-toc-counter ez-toc-light-blue ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title ez-toc-toggle\" style=\"cursor:pointer\">\u76ee\u5f55<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/blog.iyatt.com\/?p=19945\/#%E6%B5%8B%E8%AF%95%E7%8E%AF%E5%A2%83\" >\u6d4b\u8bd5\u73af\u5883<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/blog.iyatt.com\/?p=19945\/#%E7%A0%94%E7%A9%B6\" >\u7814\u7a76<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/blog.iyatt.com\/?p=19945\/#%E7%BB%93%E8%AE%BA\" >\u7ed3\u8bba<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/blog.iyatt.com\/?p=19945\/#%E5%BA%94%E7%94%A8\" >\u5e94\u7528<\/a><ul class='ez-toc-list-level-2' ><li class='ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/blog.iyatt.com\/?p=19945\/#%E6%89%93%E5%8C%85%E7%8A%B6%E6%80%81%E4%B8%8E%E8%B7%AF%E5%BE%84\" >\u6253\u5305\u72b6\u6001\u4e0e\u8def\u5f84<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/blog.iyatt.com\/?p=19945\/#%E7%9F%AD%E8%B7%AF%E5%BE%84%E8%BD%AC%E9%95%BF%E8%B7%AF%E5%BE%84\" >\u77ed\u8def\u5f84\u8f6c\u957f\u8def\u5f84<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n<h1><span class=\"ez-toc-section\" id=\"%E6%B5%8B%E8%AF%95%E7%8E%AF%E5%A2%83\"><\/span>\u6d4b\u8bd5\u73af\u5883<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<ul>\n<li>Windows 11 25H2<\/li>\n<li>Python 3.14.5<\/li>\n<li>Nuitka 4.2<\/li>\n<li>PowerShell 7.6.5<\/li>\n<\/ul>\n<h1><span class=\"ez-toc-section\" id=\"%E7%A0%94%E7%A9%B6\"><\/span>\u7814\u7a76<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<p>\u4e3b\u8981\u6d4b\u8bd5\u5185\u5bb9\u7684\u7b80\u5355\u6e90\u7801<\/p>\n<pre><code class=\"language-python\">import sys\n\nisCompiled = &#039;__compiled__&#039; in globals()\nprint(f&#039;\u662f\u5426\u6253\u5305: {isCompiled}&#039;)\n\nprint(f&#039;sys.exeutable: {sys.executable}&#039;)\nprint(f&#039;sys.argv[0]: {sys.argv[0]}&#039;)\nprint(f&#039;__file__: {__file__}&#039;)\n\ncompiledObj = globals().get(&#039;__compiled__&#039;)\nif compiledObj is not None:\n    print(f&#039;onefile: {compiledObj.onefile}&#039;)\n    print(f&#039;original_argv0: {compiledObj.original_argv0}&#039;)\n    print(f&#039;containing_dir: {compiledObj.containing_dir}&#039;)\n\ninput(&#039;\u6309\u56de\u8f66\u952e\u7ee7\u7eed...&#039;)<\/code><\/pre>\n<p>\u5b8c\u6574\u6d4b\u8bd5\u60c5\u51b5\u7684\u4e00\u952e PowerShell \u811a\u672c<\/p>\n<pre><code class=\"language-powershell\"># ==========================================\n# Nuitka \u81ea\u52a8\u5316\u6d4b\u8bd5\u4e0e\u7ed3\u679c\u9a8c\u8bc1\u811a\u672c\n# ==========================================\n\n$ErrorActionPreference = &quot;Stop&quot;\n$ScriptDir = Split-Path -Parent $MyInvocation.MyCommand.Definition\nSet-Location $ScriptDir\n\n# 1. \u521d\u59cb\u5316\u865a\u62df\u73af\u5883\u4e0e\u4f9d\u8d56\nif (-not (Test-Path &quot;venv&quot;)) {\n    Write-Host &quot;Creating Virtual Environment...&quot; -ForegroundColor Green\n    python -m venv venv\n}\n\n&amp; .\\venv\\Scripts\\Activate.ps1\npython -m pip install --upgrade pip | Out-Null\npip install nuitka==4.2 | Out-Null\n\n# 2. \u51c6\u5907\u6d4b\u8bd5\u9879\u76ee\u6e90\u7801\u4e0e\u76ee\u5f55\u7ed3\u6784\nWrite-Host &quot;Preparing test source files...&quot; -ForegroundColor Green\nNew-Item -ItemType Directory -Force -Path &quot;testmodule&quot; | Out-Null\n\n# \u3010\u4e3b\u7a0b\u5e8f\u6a21\u5f0f\u3011\u6838\u5fc3\u4ee3\u7801 (main.py)\n@&#039;\nimport sys\n\nisCompiled = &#039;__compiled__&#039; in globals()\nexecutable = sys.executable\nargv0 = sys.argv[0]\nfile = __file__\n\ncompiledObj = globals().get(&#039;__compiled__&#039;)\nonefile = None\noriginal_argv0 = None\ncontaining_dir = None\nif compiledObj is not None:\n    onefile = compiledObj.onefile\n    original_argv0 = compiledObj.original_argv0\n    containing_dir = compiledObj.containing_dir\n\nprint(f&#039;\u662f\u5426\u6253\u5305: {isCompiled}&#039;)\nprint(f&#039;executable: {executable}&#039;)\nprint(f&#039;argv0: {argv0}&#039;)\nprint(f&#039;__file__: {file}&#039;)\nprint(f&#039;onefile: {onefile}&#039;)\nprint(f&#039;original_argv0: {original_argv0}&#039;)\nprint(f&#039;containing_dir: {containing_dir}&#039;)\n&#039;@ | Set-Content -Path &quot;main.py&quot; -Encoding UTF8\n\n# \u3010\u6a21\u5757\u6a21\u5f0f\u3011\u6a21\u5757\u4ee3\u7801 (testmodule\/testmodule.py)\n@&#039;\nimport sys\n\nisCompiled = &#039;__compiled__&#039; in globals()\nexecutable = sys.executable\nargv0 = sys.argv[0]\nfile = __file__\n\ncompiledObj = globals().get(&#039;__compiled__&#039;)\nonefile = None\noriginal_argv0 = None\ncontaining_dir = None\nif compiledObj is not None:\n    onefile = compiledObj.onefile\n    original_argv0 = compiledObj.original_argv0\n    containing_dir = compiledObj.containing_dir\n&#039;@ | Set-Content -Path &quot;testmodule\\testmodule.py&quot; -Encoding UTF8\n\n# \u3010\u6a21\u5757\u6a21\u5f0f\u3011\u4e3b\u5165\u53e3\u4ee3\u7801 (mainmodule.py)\n@&#039;\nfrom testmodule import testmodule\n\nprint(f&#039;\u662f\u5426\u6253\u5305: {testmodule.isCompiled}&#039;)\nprint(f&#039;executable: {testmodule.executable}&#039;)\nprint(f&#039;argv0: {testmodule.argv0}&#039;)\nprint(f&#039;__file__: {testmodule.file}&#039;)\nprint(f&#039;onefile: {testmodule.onefile}&#039;)\nprint(f&#039;original_argv0: {testmodule.original_argv0}&#039;)\nprint(f&#039;containing_dir: {testmodule.containing_dir}&#039;)\n&#039;@ | Set-Content -Path &quot;mainmodule.py&quot; -Encoding UTF8\n\n# 3. Nuitka \u6253\u5305\u6784\u5efa\uff08\u5171 4 \u4e2a Target\uff09\nWrite-Host &quot;`n==========================================&quot; -ForegroundColor Cyan\nWrite-Host &quot; Building Executables with Nuitka...&quot; -ForegroundColor Cyan\nWrite-Host &quot;==========================================&quot; -ForegroundColor Cyan\n\n# \u6b63\u786e\u7684\u903b\u8f91\uff1a\u591a\u6587\u4ef6\u6253\u5305\u4f7f\u7528 --standalone\uff1b\u5355\u6587\u4ef6\u6253\u5305\u540c\u65f6\u4f7f\u7528 --standalone \u548c --onefile\n$BuildTasks = @(\n    @{ Entry = &quot;main.py&quot;;       OutputDir = &quot;dist_main_standalone&quot;; Flags = @(&quot;--standalone&quot;);              Name = &quot;main.exe&quot; },\n    @{ Entry = &quot;main.py&quot;;       OutputDir = &quot;dist_main_onefile&quot;;    Flags = @(&quot;--standalone&quot;, &quot;--onefile&quot;); Name = &quot;mainonefile.exe&quot; },\n    @{ Entry = &quot;mainmodule.py&quot;; OutputDir = &quot;dist_mod_standalone&quot;;  Flags = @(&quot;--standalone&quot;);              Name = &quot;mainmodule.exe&quot; },\n    @{ Entry = &quot;mainmodule.py&quot;; OutputDir = &quot;dist_mod_onefile&quot;;     Flags = @(&quot;--standalone&quot;, &quot;--onefile&quot;); Name = &quot;mainmoduleonefile.exe&quot; }\n)\n\nforeach ($Task in $BuildTasks) {\n    $entryFile = $Task[&quot;Entry&quot;]\n    $outDir    = $Task[&quot;OutputDir&quot;]\n    $flags     = $Task[&quot;Flags&quot;]\n    $exeName   = $Task[&quot;Name&quot;]\n\n    $flagsStr = $flags -join &quot; &quot;\n    Write-Host &quot;Building $exeName [$flagsStr]...&quot; -ForegroundColor Yellow\n    $StartTime = Get-Date\n\n    nuitka `\n        $flags `\n        --follow-imports `\n        --assume-yes-for-downloads `\n        &quot;--output-dir=$outDir&quot; `\n        &quot;--output-filename=$exeName&quot; `\n        $entryFile | Out-Null\n\n    $Elapsed = (New-TimeSpan -Start $StartTime -End (Get-Date)).TotalSeconds\n    Write-Host &quot;Built $exeName in [ $(([math]::Round($Elapsed, 2)))s ]&quot; -ForegroundColor DarkGreen\n}\n\n# 4. \u6267\u884c\u6d4b\u8bd5\u4e0e\u7ed3\u679c\u8f93\u51fa\nWrite-Host &quot;`n==========================================&quot; -ForegroundColor Cyan\nWrite-Host &quot; Executing Test Cases &amp; Gathering Results &quot; -ForegroundColor Cyan\nWrite-Host &quot;==========================================&quot; -ForegroundColor Cyan\n\nfunction Run-TestAndPrint {\n    param(\n        [string]$Title,\n        [string]$SearchDir,\n        [string]$ExeName,\n        [string]$DirectCommand = $null,\n        [string[]]$ArgsList = @()\n    )\n    Write-Host &quot;`n------------------------------------------&quot; -ForegroundColor White\n    Write-Host &quot; Case: $Title&quot; -ForegroundColor Yellow\n    Write-Host &quot;------------------------------------------&quot; -ForegroundColor White\n\n    if ($DirectCommand) {\n        &amp; $DirectCommand $ArgsList\n    } else {\n        # \u5728\u8f93\u51fa\u76ee\u5f55\u4e0b\u5bfb\u627e\u751f\u6210\u7684 EXE\n        $TargetFile = Get-ChildItem -Path $SearchDir -Filter $ExeName -Recurse -File -ErrorAction SilentlyContinue | Select-Object -First 1\n        if ($TargetFile) {\n            &amp; $TargetFile.FullName $ArgsList\n        } else {\n            Write-Host &quot;\u9519\u8bef: \u672a\u5728\u76ee\u5f55 $SearchDir \u4e0b\u627e\u5230\u751f\u6210\u7684\u53ef\u6267\u884c\u6587\u4ef6 $ExeName&quot; -ForegroundColor Red\n        }\n    }\n}\n\n# \u573a\u666f 1\uff1a\u4e3b\u7a0b\u5e8f - \u6e90\u7801\u6267\u884c\nRun-TestAndPrint -Title &quot;\u4e3b\u7a0b\u5e8f - \u6e90\u7801\u6267\u884c&quot; -DirectCommand &quot;.\\venv\\Scripts\\python.exe&quot; -ArgsList @(&quot;.\\main.py&quot;)\n\n# \u573a\u666f 2\uff1a\u4e3b\u7a0b\u5e8f - \u591a\u6587\u4ef6\u6253\u5305\u6267\u884c (--standalone)\nRun-TestAndPrint -Title &quot;\u4e3b\u7a0b\u5e8f - \u591a\u6587\u4ef6\u6253\u5305\u6267\u884c (--standalone)&quot; -SearchDir &quot;dist_main_standalone&quot; -ExeName &quot;main.exe&quot;\n\n# \u573a\u666f 3\uff1a\u4e3b\u7a0b\u5e8f - \u5355\u6587\u4ef6\u6253\u5305\u6267\u884c (--standalone --onefile)\nRun-TestAndPrint -Title &quot;\u4e3b\u7a0b\u5e8f - \u5355\u6587\u4ef6\u6253\u5305\u6267\u884c (--standalone --onefile)&quot; -SearchDir &quot;dist_main_onefile&quot; -ExeName &quot;mainonefile.exe&quot;\n\n# \u573a\u666f 4\uff1a\u6a21\u5757 - \u6e90\u7801\u6267\u884c\nRun-TestAndPrint -Title &quot;\u6a21\u5757 - \u6e90\u7801\u6267\u884c&quot; -DirectCommand &quot;.\\venv\\Scripts\\python.exe&quot; -ArgsList @(&quot;.\\mainmodule.py&quot;)\n\n# \u573a\u666f 5\uff1a\u6a21\u5757 - \u591a\u6587\u4ef6\u6253\u5305\u6267\u884c (--standalone)\nRun-TestAndPrint -Title &quot;\u6a21\u5757 - \u591a\u6587\u4ef6\u6253\u5305\u6267\u884c (--standalone)&quot; -SearchDir &quot;dist_mod_standalone&quot; -ExeName &quot;mainmodule.exe&quot;\n\n# \u573a\u666f 6\uff1a\u6a21\u5757 - \u5355\u6587\u4ef6\u6253\u5305\u6267\u884c (--standalone --onefile)\nRun-TestAndPrint -Title &quot;\u6a21\u5757 - \u5355\u6587\u4ef6\u6253\u5305\u6267\u884c (--standalone --onefile)&quot; -SearchDir &quot;dist_mod_onefile&quot; -ExeName &quot;mainmoduleonefile.exe&quot;\n\nWrite-Host &quot;`n==========================================&quot; -ForegroundColor Green\nWrite-Host &quot; All 6 Test Cases Completed Successfully!&quot; -ForegroundColor Green\nWrite-Host &quot;==========================================&quot; -ForegroundColor Green<\/code><\/pre>\n<p>\u6d4b\u8bd5\u8f93\u51fa\u7ed3\u679c<\/p>\n<pre><code class=\"language-powershell\">------------------------------------------\n Case: \u4e3b\u7a0b\u5e8f - \u6e90\u7801\u6267\u884c\n------------------------------------------\n\u662f\u5426\u6253\u5305: False\nexecutable: C:\\work\\development\\nuitka\\venv\\Scripts\\python.exe\nargv0: .\\main.py\n__file__: C:\\work\\development\\nuitka\\main.py\nonefile: None\noriginal_argv0: None\ncontaining_dir: None\n\n------------------------------------------\n Case: \u4e3b\u7a0b\u5e8f - \u591a\u6587\u4ef6\u6253\u5305\u6267\u884c (--standalone)\n------------------------------------------\n\u662f\u5426\u6253\u5305: True\nexecutable: C:\\work\\DEVELO~1\\nuitka\\DIST_M~1\\MAIN~1.DIS\\python.exe\nargv0: C:\\work\\development\\nuitka\\dist_main_standalone\\main.dist\\main.exe\n__file__: C:\\work\\DEVELO~1\\nuitka\\DIST_M~1\\MAIN~1.DIS\\main.py\nonefile: False\noriginal_argv0: C:\\work\\development\\nuitka\\dist_main_standalone\\main.dist\\main.exe\ncontaining_dir: C:\\work\\DEVELO~1\\nuitka\\DIST_M~1\n\n------------------------------------------\n Case: \u4e3b\u7a0b\u5e8f - \u5355\u6587\u4ef6\u6253\u5305\u6267\u884c (--standalone --onefile)\n------------------------------------------\n\u662f\u5426\u6253\u5305: True\nexecutable: C:\\Users\\ZHAOHO~1\\AppData\\Local\\Temp\\onefile_36000_032854_Eto8l3VYNbU\\python.exe\nargv0: C:\\work\\development\\nuitka\\dist_main_onefile\\mainonefile.exe\n__file__: C:\\Users\\ZHAOHO~1\\AppData\\Local\\Temp\\onefile_36000_032854_Eto8l3VYNbU\\main.py\nonefile: True\noriginal_argv0: C:\\work\\development\\nuitka\\dist_main_onefile\\mainonefile.exe\ncontaining_dir: C:\\work\\development\\nuitka\\dist_main_onefile\n\n------------------------------------------\n Case: \u6a21\u5757 - \u6e90\u7801\u6267\u884c\n------------------------------------------\n\u662f\u5426\u6253\u5305: False\nexecutable: C:\\work\\development\\nuitka\\venv\\Scripts\\python.exe\nargv0: .\\mainmodule.py\n__file__: C:\\work\\development\\nuitka\\testmodule\\testmodule.py\nonefile: None\noriginal_argv0: None\ncontaining_dir: None\n\n------------------------------------------\n Case: \u6a21\u5757 - \u591a\u6587\u4ef6\u6253\u5305\u6267\u884c (--standalone)\n------------------------------------------\n\u662f\u5426\u6253\u5305: True\nexecutable: C:\\work\\DEVELO~1\\nuitka\\DIST_M~3\\MAINMO~1.DIS\\python.exe\nargv0: C:\\work\\development\\nuitka\\dist_mod_standalone\\mainmodule.dist\\mainmodule.exe\n__file__: C:\\work\\DEVELO~1\\nuitka\\DIST_M~3\\MAINMO~1.DIS\\testmodule\\testmodule.py\nonefile: False\noriginal_argv0: C:\\work\\development\\nuitka\\dist_mod_standalone\\mainmodule.dist\\mainmodule.exe\ncontaining_dir: C:\\work\\DEVELO~1\\nuitka\\DIST_M~3\n\n------------------------------------------\n Case: \u6a21\u5757 - \u5355\u6587\u4ef6\u6253\u5305\u6267\u884c (--standalone --onefile)\n------------------------------------------\n\u662f\u5426\u6253\u5305: True\nexecutable: C:\\Users\\ZHAOHO~1\\AppData\\Local\\Temp\\onefile_6060_434065_GGL3bKO_4x8\\python.exe\nargv0: C:\\work\\development\\nuitka\\dist_mod_onefile\\mainmoduleonefile.exe\n__file__: C:\\Users\\ZHAOHO~1\\AppData\\Local\\Temp\\onefile_6060_434065_GGL3bKO_4x8\\testmodule\\testmodule.py\nonefile: True\noriginal_argv0: C:\\work\\development\\nuitka\\dist_mod_onefile\\mainmoduleonefile.exe\ncontaining_dir: C:\\work\\development\\nuitka\\dist_mod_onefile<\/code><\/pre>\n<h1><span class=\"ez-toc-section\" id=\"%E7%BB%93%E8%AE%BA\"><\/span>\u7ed3\u8bba<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<table>\n<thead>\n<tr>\n<th style=\"text-align: left;\">\u5c5e\u6027 \/ \u53d8\u91cf<\/th>\n<th style=\"text-align: left;\">1. \u4e3b\u7a0b\u5e8f &#8211; \u6e90\u7801<\/th>\n<th style=\"text-align: left;\">2. \u4e3b\u7a0b\u5e8f &#8211; \u591a\u6587\u4ef6 (<code>--standalone<\/code>)<\/th>\n<th style=\"text-align: left;\">3. \u4e3b\u7a0b\u5e8f &#8211; \u5355\u6587\u4ef6 (<code>--standalone --onefile<\/code>)<\/th>\n<th style=\"text-align: left;\">4. \u6a21\u5757 &#8211; \u6e90\u7801<\/th>\n<th style=\"text-align: left;\">5. \u6a21\u5757 &#8211; \u591a\u6587\u4ef6 (<code>--standalone<\/code>)<\/th>\n<th style=\"text-align: left;\">6. \u6a21\u5757 &#8211; \u5355\u6587\u4ef6 (<code>--standalone --onefile<\/code>)<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"text-align: left;\"><strong><code>\u662f\u5426\u6253\u5305<\/code><\/strong><br \/><code>&#039;__compiled__&#039; in globals()<\/code><\/td>\n<td style=\"text-align: left;\"><code>False<\/code><\/td>\n<td style=\"text-align: left;\"><code>True<\/code><\/td>\n<td style=\"text-align: left;\"><code>True<\/code><\/td>\n<td style=\"text-align: left;\"><code>False<\/code><\/td>\n<td style=\"text-align: left;\"><code>True<\/code><\/td>\n<td style=\"text-align: left;\"><code>True<\/code><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\"><strong><code>executable<\/code><\/strong><br \/><code>sys.executable<\/code><\/td>\n<td style=\"text-align: left;\"><code>...\\venv\\Scripts\\python.exe<\/code><br \/><em>(\u865a\u62df\u73af\u5883 Python)<\/em><\/td>\n<td style=\"text-align: left;\"><code>...\\dist_main_standalone\\main.dist\\python.exe<\/code><br \/><em>(\u5206\u53d1\u76ee\u5f55\u5185\u5efa Python)<\/em><\/td>\n<td style=\"text-align: left;\"><code>...\\AppData\\Local\\Temp\\onefile_36000_...\\python.exe<\/code><br \/><em>(Temp \u4e34\u65f6\u89e3\u538b\u76ee\u5f55)<\/em><\/td>\n<td style=\"text-align: left;\"><code>...\\venv\\Scripts\\python.exe<\/code><br \/><em>(\u865a\u62df\u73af\u5883 Python)<\/em><\/td>\n<td style=\"text-align: left;\"><code>...\\dist_mod_standalone\\mainmodule.dist\\python.exe<\/code><br \/><em>(\u5206\u53d1\u76ee\u5f55\u5185\u5efa Python)<\/em><\/td>\n<td style=\"text-align: left;\"><code>...\\AppData\\Local\\Temp\\onefile_6060_...\\python.exe<\/code><br \/><em>(Temp \u4e34\u65f6\u89e3\u538b\u76ee\u5f55)<\/em><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\"><strong><code>argv0<\/code><\/strong><br \/><code>sys.argv[0]<\/code><\/td>\n<td style=\"text-align: left;\"><code>.\\main.py<\/code><br \/><em>(\u547d\u4ee4\u884c\u76f8\u5bf9\u8def\u5f84)<\/em><\/td>\n<td style=\"text-align: left;\"><code>...\\dist_main_standalone\\main.dist\\main.exe<\/code><br \/><em>(EXE \u7edd\u5bf9\u8def\u5f84)<\/em><\/td>\n<td style=\"text-align: left;\"><code>...\\dist_main_onefile\\mainonefile.exe<\/code><br \/><em>(EXE \u7edd\u5bf9\u8def\u5f84)<\/em><\/td>\n<td style=\"text-align: left;\"><code>.\\mainmodule.py<\/code><br \/><em>(\u547d\u4ee4\u884c\u76f8\u5bf9\u8def\u5f84)<\/em><\/td>\n<td style=\"text-align: left;\"><code>...\\dist_mod_standalone\\mainmodule.dist\\mainmodule.exe<\/code><br \/><em>(EXE \u7edd\u5bf9\u8def\u5f84)<\/em><\/td>\n<td style=\"text-align: left;\"><code>...\\dist_mod_onefile\\mainmoduleonefile.exe<\/code><br \/><em>(EXE \u7edd\u5bf9\u8def\u5f84)<\/em><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\"><strong><code>__file__<\/code><\/strong><\/td>\n<td style=\"text-align: left;\"><code>...\\nuitka\\main.py<\/code><br \/><em>(\u4e3b\u7a0b\u5e8f\u6e90\u7801\u8def\u5f84)<\/em><\/td>\n<td style=\"text-align: left;\"><code>...\\dist_main_standalone\\main.dist\\main.py<\/code><br \/><em>(\u5206\u53d1\u76ee\u5f55\u6620\u5c04\u8def\u5f84)<\/em><\/td>\n<td style=\"text-align: left;\"><code>...\\AppData\\Local\\Temp\\onefile_36000_...\\main.py<\/code><br \/><em>(Temp \u4e34\u65f6\u89e3\u538b\u8def\u5f84)<\/em><\/td>\n<td style=\"text-align: left;\"><code>...\\nuitka\\testmodule\\testmodule.py<\/code><br \/><em>(\u88ab\u8c03\u6a21\u5757\u6e90\u7801\u8def\u5f84)<\/em><\/td>\n<td style=\"text-align: left;\"><code>...\\dist_mod_standalone\\mainmodule.dist\\testmodule\\testmodule.py<\/code><br \/><em>(\u5206\u53d1\u76ee\u5f55\u6a21\u5757\u6620\u5c04\u8def\u5f84)<\/em><\/td>\n<td style=\"text-align: left;\"><code>...\\AppData\\Local\\Temp\\onefile_6060_...\\testmodule\\testmodule.py<\/code><br \/><em>(Temp \u4e34\u65f6\u89e3\u538b\u6a21\u5757\u8def\u5f84)<\/em><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\"><strong><code>onefile<\/code><\/strong><br \/><code>__compiled__.onefile<\/code><\/td>\n<td style=\"text-align: left;\"><code>None<\/code><\/td>\n<td style=\"text-align: left;\"><code>False<\/code><\/td>\n<td style=\"text-align: left;\"><code>True<\/code><\/td>\n<td style=\"text-align: left;\"><code>None<\/code><\/td>\n<td style=\"text-align: left;\"><code>False<\/code><\/td>\n<td style=\"text-align: left;\"><code>True<\/code><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\"><strong><code>original_argv0<\/code><\/strong><br \/><code>__compiled__.original_argv0<\/code><\/td>\n<td style=\"text-align: left;\"><code>None<\/code><\/td>\n<td style=\"text-align: left;\"><code>...\\dist_main_standalone\\main.dist\\main.exe<\/code><br \/><em>(\u8c03\u7528 EXE \u7edd\u5bf9\u8def\u5f84)<\/em><\/td>\n<td style=\"text-align: left;\"><code>...\\dist_main_onefile\\mainonefile.exe<\/code><br \/><em>(\u8c03\u7528 EXE \u7edd\u5bf9\u8def\u5f84)<\/em><\/td>\n<td style=\"text-align: left;\"><code>None<\/code><\/td>\n<td style=\"text-align: left;\"><code>...\\dist_mod_standalone\\mainmodule.dist\\mainmodule.exe<\/code><br \/><em>(\u8c03\u7528 EXE \u7edd\u5bf9\u8def\u5f84)<\/em><\/td>\n<td style=\"text-align: left;\"><code>...\\dist_mod_onefile\\mainmoduleonefile.exe<\/code><br \/><em>(\u8c03\u7528 EXE \u7edd\u5bf9\u8def\u5f84)<\/em><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\"><strong><code>containing_dir<\/code><\/strong><br \/><code>__compiled__.containing_dir<\/code><\/td>\n<td style=\"text-align: left;\"><code>None<\/code><\/td>\n<td style=\"text-align: left;\"><code>...\\dist_main_standalone\\main.dist<\/code><br \/><em>(\u5206\u53d1\u6587\u4ef6\u5939\u6839\u76ee\u5f55)<\/em><\/td>\n<td style=\"text-align: left;\"><code>...\\dist_main_onefile<\/code><br \/><em>(\u5916\u90e8 EXE \u6240\u5728\u76ee\u5f55)<\/em><\/td>\n<td style=\"text-align: left;\"><code>None<\/code><\/td>\n<td style=\"text-align: left;\"><code>...\\dist_mod_standalone\\mainmodule.dist<\/code><br \/><em>(\u5206\u53d1\u6587\u4ef6\u5939\u6839\u76ee\u5f55)<\/em><\/td>\n<td style=\"text-align: left;\"><code>...\\dist_mod_onefile<\/code><br \/><em>(\u5916\u90e8 EXE \u6240\u5728\u76ee\u5f55)<\/em><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h1><span class=\"ez-toc-section\" id=\"%E5%BA%94%E7%94%A8\"><\/span>\u5e94\u7528<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<h2><span class=\"ez-toc-section\" id=\"%E6%89%93%E5%8C%85%E7%8A%B6%E6%80%81%E4%B8%8E%E8%B7%AF%E5%BE%84\"><\/span>\u6253\u5305\u72b6\u6001\u4e0e\u8def\u5f84<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>\u6ce8\u610f\u5e94\u7528\u4e2d Nuitka \u662f\u4f7f\u7528\u4e86 &#8211;standalone \u53c2\u6570\u7684\u60c5\u666f\u4e0b\uff08\u72ec\u7acb\u6a21\u5f0f\u4e0e\u6e90\u7801\u8131\u94a9\uff0c\u4e14\u9644\u5e26\u6253\u5305\u5b8c\u6574\u4f9d\u8d56\uff09\uff0c\u662f\u5426\u4f7f\u7528\u8fd9\u4e2a\u53c2\u6570\uff0c__file__ \u7684\u503c\u662f\u6709\u533a\u522b\u7684\u3002<\/p>\n<p>\u793a\u4f8b\u6e90\u7801\uff1atest.py<\/p>\n<pre><code class=\"language-python\">import os\nimport sys\n\nisCompiled = &#039;__compiled__&#039; in globals()\nprint(f&#039;\u662f\u5426\u5df2\u6253\u5305\uff1a{isCompiled}&#039;)\n\nprogramFilePath = os.path.abspath(sys.argv[0])\nprint(f&#039;\u7a0b\u5e8f\u6587\u4ef6\u7edd\u5bf9\u8def\u5f84\uff08\u65b9\u6cd5\u4e00\uff09\uff1a{programFilePath}&#039;)\n\nfilePath = __file__\nprint(f&#039;\u5f53\u524d\u6587\u4ef6\u7edd\u5bf9\u8def\u5f84\uff08\u8c03\u7528\u8005\u6e90\u7801\u6587\u4ef6\u6216\u8c03\u7528\u8005\u6e90\u7801\u6587\u4ef6\u6253\u5305\u540e\u6620\u5c04\u8def\u5f84\uff09\uff1a{filePath}&#039;)\n\nappDir = os.path.dirname(__file__)\nprint(f&#039;\u7a0b\u5e8f\u8fd0\u884c\u65f6\u53ef\u8bbf\u95ee\u6253\u5305\u8d44\u6e90\u7684\u6839\u76ee\u5f55\uff1a{appDir}&#039;)\n\ncompiledObj = globals().get(&#039;__compiled__&#039;)\nif compiledObj is not None:\n    programFilePath = compiledObj.original_argv0\n    print(f&#039;\u7a0b\u5e8f\u6587\u4ef6\u7edd\u5bf9\u8def\u5f84\uff08\u65b9\u6cd5\u4e8c\uff09\uff1a{programFilePath}&#039;)\n\n    programFileDir = compiledObj.containing_dir\n    print(f&#039;\u7a0b\u5e8f\u6587\u4ef6\u6240\u5728\u76ee\u5f55\uff1a{programFileDir}&#039;)\n\n    onefile = compiledObj.onefile\n    print(f&#039;\u662f\u5426\u4e3a\u5355\u6587\u4ef6\u6253\u5305\uff1a{onefile}&#039;)\nelse: # \u672a\u6253\u5305\n    pythonPath = sys.executable\n    print(f&#039;Python \u89e3\u91ca\u5668\u8def\u5f84\uff1a{pythonPath}&#039;)\n\n    executableCommandString = sys.executable + &#039; &#039; + os.path.abspath(sys.argv[0])\n    print(f&#039;\u53ef\u6267\u884c\u547d\u4ee4\u5b57\u7b26\u4e32\uff1a{executableCommandString}&#039;)<\/code><\/pre>\n<p>\u6e90\u7801\u8fd0\u884c\u7ed3\u679c<\/p>\n<pre><code class=\"language-powershell\">\u662f\u5426\u5df2\u6253\u5305\uff1aFalse\n\u7a0b\u5e8f\u6587\u4ef6\u7edd\u5bf9\u8def\u5f84\uff08\u65b9\u6cd5\u4e00\uff09\uff1aC:\\work\\development\\nuitka\\test.py\n\u5f53\u524d\u6587\u4ef6\u7edd\u5bf9\u8def\u5f84\uff08\u8c03\u7528\u8005\u6e90\u7801\u6587\u4ef6\u6216\u8c03\u7528\u8005\u6e90\u7801\u6587\u4ef6\u6253\u5305\u540e\u6620\u5c04\u8def\u5f84\uff09\uff1aC:\\work\\development\\nuitka\\test.py\n\u7a0b\u5e8f\u8fd0\u884c\u65f6\u53ef\u8bbf\u95ee\u6253\u5305\u8d44\u6e90\u7684\u6839\u76ee\u5f55\uff1aC:\\work\\development\\nuitka\nPython \u89e3\u91ca\u5668\u8def\u5f84\uff1aC:\\work\\development\\nuitka\\venv\\Scripts\\python.exe\n\u53ef\u6267\u884c\u547d\u4ee4\u5b57\u7b26\u4e32\uff1aC:\\work\\development\\nuitka\\venv\\Scripts\\python.exe C:\\work\\development\\nuitka\\test.py<\/code><\/pre>\n<p>\u5355\u6587\u4ef6\u6253\u5305\u8fd0\u884c<\/p>\n<pre><code class=\"language-powershell\">$startTime = Get-Date\n\npython -m venv venv\n.\\venv\\Scripts\\Activate.ps1\npython.exe -m pip install --upgrade pip\npip install nuitka==4.2\n\nnuitka `\n--remove-output `\n--standalone `\n--onefile `\n--output-dir=dist `\n--output-filename=test `\n.\\test.py\n\n$endTime = Get-Date\n$elapsedTime = New-TimeSpan -Start $startTime -End $endTime\nWrite-Output &quot;\u7a0b\u5e8f\u6784\u5efa\u7528\u65f6\uff1a$($elapsedTime.TotalSeconds) \u79d2&quot;\n\n.\\dist\\test.exe<\/code><\/pre>\n<p>\u5355\u6587\u4ef6\u6253\u5305\u8fd0\u884c\u7ed3\u679c<\/p>\n<pre><code class=\"language-powershell\">\u662f\u5426\u5df2\u6253\u5305\uff1aTrue\n\u7a0b\u5e8f\u6587\u4ef6\u7edd\u5bf9\u8def\u5f84\uff08\u65b9\u6cd5\u4e00\uff09\uff1aC:\\work\\development\\nuitka\\dist\\test.exe\n\u5f53\u524d\u6587\u4ef6\u7edd\u5bf9\u8def\u5f84\uff08\u8c03\u7528\u8005\u6e90\u7801\u6587\u4ef6\u6216\u8c03\u7528\u8005\u6e90\u7801\u6587\u4ef6\u6253\u5305\u540e\u6620\u5c04\u8def\u5f84\uff09\uff1aC:\\Users\\ZHAOHO~1\\AppData\\Local\\Temp\\onefile_37876_228418_z8xZM769z-4\\test.py\n\u7a0b\u5e8f\u8fd0\u884c\u65f6\u53ef\u8bbf\u95ee\u6253\u5305\u8d44\u6e90\u7684\u6839\u76ee\u5f55\uff1aC:\\Users\\ZHAOHO~1\\AppData\\Local\\Temp\\onefile_37876_228418_z8xZM769z-4\n\u7a0b\u5e8f\u6587\u4ef6\u7edd\u5bf9\u8def\u5f84\uff08\u65b9\u6cd5\u4e8c\uff09\uff1aC:\\work\\development\\nuitka\\dist\\test.exe\n\u7a0b\u5e8f\u6587\u4ef6\u6240\u5728\u76ee\u5f55\uff1aC:\\work\\development\\nuitka\\dist\n\u662f\u5426\u4e3a\u5355\u6587\u4ef6\u6253\u5305\uff1aTrue<\/code><\/pre>\n<p>\u591a\u6587\u4ef6\u6253\u5305\u8fd0\u884c<\/p>\n<pre><code class=\"language-powershell\">$startTime = Get-Date\n\npython -m venv venv\n.\\venv\\Scripts\\Activate.ps1\npython.exe -m pip install --upgrade pip\npip install nuitka==4.2\n\nnuitka `\n--remove-output `\n--standalone `\n--output-dir=dist `\n--output-filename=test `\n.\\test.py\n\n$endTime = Get-Date\n$elapsedTime = New-TimeSpan -Start $startTime -End $endTime\nWrite-Output &quot;\u7a0b\u5e8f\u6784\u5efa\u7528\u65f6\uff1a$($elapsedTime.TotalSeconds) \u79d2&quot;\n\n.\\dist\\test.dist\\test.exe<\/code><\/pre>\n<p>\u591a\u6587\u4ef6\u6253\u5305\u8fd0\u884c\u7ed3\u679c<\/p>\n<pre><code class=\"language-powershell\">\u662f\u5426\u5df2\u6253\u5305\uff1aTrue\n\u7a0b\u5e8f\u6587\u4ef6\u7edd\u5bf9\u8def\u5f84\uff08\u65b9\u6cd5\u4e00\uff09\uff1aC:\\work\\development\\nuitka\\dist\\test.dist\\test.exe\n\u5f53\u524d\u6587\u4ef6\u7edd\u5bf9\u8def\u5f84\uff08\u8c03\u7528\u8005\u6e90\u7801\u6587\u4ef6\u6216\u8c03\u7528\u8005\u6e90\u7801\u6587\u4ef6\u6253\u5305\u540e\u6620\u5c04\u8def\u5f84\uff09\uff1aC:\\work\\DEVELO~1\\nuitka\\dist\\TEST~1.DIS\\test.py\n\u7a0b\u5e8f\u8fd0\u884c\u65f6\u53ef\u8bbf\u95ee\u6253\u5305\u8d44\u6e90\u7684\u6839\u76ee\u5f55\uff1aC:\\work\\DEVELO~1\\nuitka\\dist\\TEST~1.DIS\n\u7a0b\u5e8f\u6587\u4ef6\u7edd\u5bf9\u8def\u5f84\uff08\u65b9\u6cd5\u4e8c\uff09\uff1aC:\\work\\development\\nuitka\\dist\\test.dist\\test.exe\n\u7a0b\u5e8f\u6587\u4ef6\u6240\u5728\u76ee\u5f55\uff1aC:\\work\\DEVELO~1\\nuitka\\dist\n\u662f\u5426\u4e3a\u5355\u6587\u4ef6\u6253\u5305\uff1aFalse<\/code><\/pre>\n<h2><span class=\"ez-toc-section\" id=\"%E7%9F%AD%E8%B7%AF%E5%BE%84%E8%BD%AC%E9%95%BF%E8%B7%AF%E5%BE%84\"><\/span>\u77ed\u8def\u5f84\u8f6c\u957f\u8def\u5f84<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>\u4e0a\u9762\u6d4b\u8bd5\u4e2d\u53ef\u4ee5\u53d1\u73b0\u90e8\u5206\u8def\u5f84\u5b58\u5728 ~ \u7b26\u53f7\uff0c\u8fd9\u4e2a\u662f Windows \u7684 8.3 \u77ed\u8def\u5f84\uff0c\u540d\u79f0\u663e\u793a\u4e3a <code>\u524d6\u4f4d\u5927\u5199\u5b57\u6bcd~\u6570\u5b57<\/code>\uff0c\u5982\u679c\u8981\u8f6c\u6362\u4e3a\u5b8c\u6574\u8def\u5f84\u53ef\u4ee5\u53c2\u8003\u4e0b\u9762\u793a\u4f8b\u3002<\/p>\n<p>test.py<\/p>\n<pre><code class=\"language-python\">from pathlib import Path\n\ndef toFullPath(filename: str) -&gt; str:\n    return str(Path(filename).resolve())\n\nprint(f&#039;\u53ef\u80fd\u5b58\u5728\u77ed\u8def\u5f84\uff1a{__file__}&#039;)\n\nfullFilePath = toFullPath(__file__)\nprint(f&#039;\u5b8c\u6574\u8def\u5f84\uff1a{fullFilePath}&#039;)<\/code><\/pre>\n<pre><code class=\"language-bash\">\u53ef\u80fd\u5b58\u5728\u77ed\u8def\u5f84\uff1aC:\\work\\DEVELO~1\\nuitka\\dist\\TEST~1.DIS\\test.py\n\u5b8c\u6574\u8def\u5f84\uff1aC:\\work\\development\\nuitka\\dist\\test.dist\\test.py<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u6d4b\u8bd5\u73af\u5883 Windows 11 25H2 Python 3.14.5 Nuitka 4.2 PowerShel [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"zakra_page_container_layout":"customizer","zakra_page_sidebar_layout":"customizer","zakra_remove_content_margin":false,"zakra_sidebar":"customizer","zakra_transparent_header":"customizer","zakra_logo":0,"zakra_main_header_style":"default","zakra_menu_item_color":"","zakra_menu_item_hover_color":null,"zakra_menu_item_active_color":null,"zakra_menu_active_style":"","zakra_page_header":true,"_lmt_disableupdate":"no","_lmt_disable":"no","footnotes":""},"categories":[1,592],"tags":[2229,2231,909,2233,2232,2230,2234,2028,2235,2137],"class_list":["post-19945","post","type-post","status-publish","format-standard","hentry","category-all","category-python","tag-nuitka","tag-pyinstaller","tag-python","tag-2233","tag-2232","tag-2230","tag-2234","tag-2028","tag-2235","tag-2137"],"modified_by":"IYATT-yx","_links":{"self":[{"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=\/wp\/v2\/posts\/19945","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=19945"}],"version-history":[{"count":38,"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=\/wp\/v2\/posts\/19945\/revisions"}],"predecessor-version":[{"id":25343,"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=\/wp\/v2\/posts\/19945\/revisions\/25343"}],"wp:attachment":[{"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=19945"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=19945"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=19945"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}