Skip to content

fix(windows): pip self-upgrade lock + GBK emoji crash on first-run venv setup#33

Open
aiking931931 wants to merge 1 commit into
PleasePrompto:masterfrom
aiking931931:fix/windows-pip-self-upgrade-and-gbk-encoding
Open

fix(windows): pip self-upgrade lock + GBK emoji crash on first-run venv setup#33
aiking931931 wants to merge 1 commit into
PleasePrompto:masterfrom
aiking931931:fix/windows-pip-self-upgrade-and-gbk-encoding

Conversation

@aiking931931
Copy link
Copy Markdown

Summary

Two Windows-only blockers stopped first-run venv setup from completing on default Windows installs (Python 3.13 / Win 11 / GBK console). Both fixes are scoped to Windows via os.name == 'nt' and stay no-op on Linux / macOS.

Bug 1 — scripts/setup_environment.py

pip.exe install --upgrade pip crashes with exit 1 on Windows even after the upgrade succeeds, because the running pip.exe is file-locked by the OS while it tries to overwrite itself. subprocess.run(check=True) then aborts the whole setup, so the venv never gets dependencies installed.

Fix: switch to the documented portable form python -m pip install --upgrade pip, drop check=True for the upgrade step, and detect success by stdout marker (Successfully installed / already satisfied). The requirements install is also moved to python -m pip for consistency.

Bug 2 — scripts/run.py

print() calls with emoji (🔧 🚀 ✅ ⚠️ ❌) crash on Windows consoles whose default encoding is GBK (cp936) or cp1252 with UnicodeEncodeError — before any of the actual logic runs.

Fix: add a tiny header right after import sys that calls sys.stdout.reconfigure(encoding="utf-8") and the same on sys.stderr when os.name == "nt". Wrapped in try/except (AttributeError, ValueError) so older Python versions and non-stream stdout (CI capture) still no-op cleanly.

Repro

Before the fix, on stock Windows 11:

```
rd /s /q .venv
python scripts/run.py auth_manager.py status
```

— first attempt aborts during pip self-upgrade (exit 1, file lock); second attempt aborts on `UnicodeEncodeError` printing the emoji header.

After the fix:

```
rd /s /q .venv
set PYTHONIOENCODING=utf-8
python scripts/run.py auth_manager.py status
```

— venv builds, deps install, status command runs and prints the emoji output without crashing.

Test plan

  • `rd /s /q .venv` followed by clean first-run on Windows 11 / Python 3.13 / GBK default console → setup completes
  • `auth_manager.py status` prints emoji-bearing output without UnicodeEncodeError
  • Verified Unix path is unchanged — the encoding hook is gated on `os.name == 'nt'`, and `python -m pip` is the documented portable form on Linux / macOS already
  • Setup remains idempotent — re-running `python scripts/run.py` on an existing venv prints `already satisfied` and exits 0

Notes

  • No new dependencies, no new files, no behaviour change on platforms other than Windows
  • The `python -m pip` form is what pip's own docs recommend for self-upgrade on Windows — this just makes the skill follow the documented portable path
  • Encoding fix is the standard Python 3.7+ pattern for the Windows console / emoji issue

🤖 Generated with Claude Code

Two Windows-only blockers prevented first-run venv setup from
completing on default Windows installs:

1. setup_environment.py — pip self-upgrade via the .exe wrapper
   crashes with exit 1 because the running pip.exe is file-locked
   by the OS even after the upgrade succeeds. Switch to the module
   entry point (python -m pip install --upgrade pip) which avoids
   the self-replace, and stop relying on check=True for the
   upgrade step. Detect success by stdout marker (Successfully
   installed / already satisfied) so a benign non-zero exit no
   longer aborts the whole setup. Also switch the requirements
   install to python -m pip for consistency.

2. run.py — print() calls with emoji characters
   (U+1F527, U+1F680, U+2705 etc) crash on Windows consoles whose
   default encoding is GBK (cp936) or cp1252, raising
   UnicodeEncodeError before any of the actual logic runs. Add
   a tiny header right after import sys that calls
   sys.stdout.reconfigure(encoding="utf-8") /
   sys.stderr.reconfigure(encoding="utf-8") when os.name == "nt".
   Wrapped in try/except so older Python versions and non-stream
   stdout (CI capture) still no-op cleanly.

Tested on Windows 11 26200 / Python 3.13 / GBK default console.
Unix (Linux / macOS) unaffected — the os.name == "nt" guard scopes
the encoding fix to Windows only; python -m pip is the documented
portable form on every platform.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant