Skip to content

{packaging} enable site in python._pth instead of deleting it#33419

Draft
YangAn-microsoft wants to merge 1 commit into
Azure:devfrom
YangAn-microsoft:fix/windows-embed-pth-site
Draft

{packaging} enable site in python._pth instead of deleting it#33419
YangAn-microsoft wants to merge 1 commit into
Azure:devfrom
YangAn-microsoft:fix/windows-embed-pth-site

Conversation

@YangAn-microsoft
Copy link
Copy Markdown
Contributor

Description

Replace del python*._pth with an idempotent append of import site to the embedded Python's ._pth file in the Windows MSI build pipeline.

Motivation

The embedded Python distribution ships with a python<XY>._pth file that, when present, switches the interpreter into isolated mode: site.py is not imported automatically and sys.path is taken solely from the ._pth contents. The long-standing workaround in build.cmd was to delete the file so the default initialization (including site.py, which adds Lib\site-packages) would run.

That trick stops working on Python 3.14+. With no ._pth present, pip's PEP 517 isolated BuildEnvironment spawns a child python.exe that can no longer locate the stdlib zip and dies very early in init_fs_encoding with:

ModuleNotFoundError: No module named 'encodings'

Keeping an explicit ._pth makes the stdlib zip and Lib\site-packages discoverable in both the parent interpreter and any spawned subprocess. Appending import site is the documented way to keep ._pth while still running site.py at startup (see Python docs — Finding modules).

Notes on the implementation:

  • if exist python*._pth (...) guards against the cmd-shell quirk where a for over a non-matching glob would otherwise iterate the literal token.
  • findstr /x "import site" ... >nul || echo import site>> %%f makes the append idempotent — re-running the script (e.g. on a cached %PYTHON_DIR%) won't keep duplicating the line.

Compatibility

  • Python 3.12 / 3.13 (current): functionally identical to the previous behaviour — site.py runs, Lib\site-packages is on sys.path, pip works.
  • Python 3.14+ (incoming): unblocks pip's isolated build subprocess.

Testing

  • Local Windows MSI build with the current embedded Python (3.12.x): pip install, pip wheel, and az CLI invocation all work as before.
  • Local Windows MSI build with Python 3.14.5: previously failed in pip subprocess with the encodings error; with this change, builds succeed end-to-end.

Relationship to PR #33313

PR #33313 (feature/python314-support) ships the full Python 3.14 upgrade and depends on this fix on Windows. Extracting the _pth change into this standalone PR keeps the fix reviewable on its own and lets it land first — it is already correct against the current Python 3.13 embedded build and is required by #33313.

History notes

N/A

@azure-client-tools-bot-prd
Copy link
Copy Markdown

azure-client-tools-bot-prd Bot commented May 22, 2026

️✔️AzureCLI-FullTest
️✔️acr
️✔️latest
️✔️3.12
️✔️3.13
️✔️acs
️✔️latest
️✔️3.12
️✔️3.13
️✔️advisor
️✔️latest
️✔️3.12
️✔️3.13
️✔️ams
️✔️latest
️✔️3.12
️✔️3.13
️✔️apim
️✔️latest
️✔️3.12
️✔️3.13
️✔️appconfig
️✔️latest
️✔️3.12
️✔️3.13
️✔️appservice
️✔️latest
️✔️3.12
️✔️3.13
️✔️aro
️✔️latest
️✔️3.12
️✔️3.13
️✔️backup
️✔️latest
️✔️3.12
️✔️3.13
️✔️batch
️✔️latest
️✔️3.12
️✔️3.13
️✔️batchai
️✔️latest
️✔️3.12
️✔️3.13
️✔️billing
️✔️latest
️✔️3.12
️✔️3.13
️✔️botservice
️✔️latest
️✔️3.12
️✔️3.13
️✔️cdn
️✔️latest
️✔️3.12
️✔️3.13
️✔️cloud
️✔️latest
️✔️3.12
️✔️3.13
️✔️cognitiveservices
️✔️latest
️✔️3.12
️✔️3.13
️✔️compute_recommender
️✔️latest
️✔️3.12
️✔️3.13
️✔️computefleet
️✔️latest
️✔️3.12
️✔️3.13
️✔️config
️✔️latest
️✔️3.12
️✔️3.13
️✔️configure
️✔️latest
️✔️3.12
️✔️3.13
️✔️consumption
️✔️latest
️✔️3.12
️✔️3.13
️✔️container
️✔️latest
️✔️3.12
️✔️3.13
️✔️containerapp
️✔️latest
️✔️3.12
️✔️3.13
️✔️core
️✔️latest
️✔️3.12
️✔️3.13
️✔️cosmosdb
️✔️latest
️✔️3.12
️✔️3.13
️✔️databoxedge
️✔️latest
️✔️3.12
️✔️3.13
️✔️dls
️✔️latest
️✔️3.12
️✔️3.13
️✔️dms
️✔️latest
️✔️3.12
️✔️3.13
️✔️eventgrid
️✔️latest
️✔️3.12
️✔️3.13
️✔️eventhubs
️✔️latest
️✔️3.12
️✔️3.13
️✔️feedback
️✔️latest
️✔️3.12
️✔️3.13
️✔️find
️✔️latest
️✔️3.12
️✔️3.13
️✔️hdinsight
️✔️latest
️✔️3.12
️✔️3.13
️✔️identity
️✔️latest
️✔️3.12
️✔️3.13
️✔️iot
️✔️latest
️✔️3.12
️✔️3.13
️✔️keyvault
️✔️latest
️✔️3.12
️✔️3.13
️✔️lab
️✔️latest
️✔️3.12
️✔️3.13
️✔️managedservices
️✔️latest
️✔️3.12
️✔️3.13
️✔️maps
️✔️latest
️✔️3.12
️✔️3.13
️✔️marketplaceordering
️✔️latest
️✔️3.12
️✔️3.13
️✔️monitor
️✔️latest
️✔️3.12
️✔️3.13
️✔️mysql
️✔️latest
️✔️3.12
️✔️3.13
️✔️netappfiles
️✔️latest
️✔️3.12
️✔️3.13
️✔️network
️✔️latest
️✔️3.12
️✔️3.13
️✔️policyinsights
️✔️latest
️✔️3.12
️✔️3.13
️✔️postgresql
️✔️latest
️✔️3.12
️✔️3.13
️✔️privatedns
️✔️latest
️✔️3.12
️✔️3.13
️✔️profile
️✔️latest
️✔️3.12
️✔️3.13
️✔️rdbms
️✔️latest
️✔️3.12
️✔️3.13
️✔️redis
️✔️latest
️✔️3.12
️✔️3.13
️✔️relay
️✔️latest
️✔️3.12
️✔️3.13
️✔️resource
️✔️latest
️✔️3.12
️✔️3.13
️✔️role
️✔️latest
️✔️3.12
️✔️3.13
️✔️search
️✔️latest
️✔️3.12
️✔️3.13
️✔️security
️✔️latest
️✔️3.12
️✔️3.13
️✔️servicebus
️✔️latest
️✔️3.12
️✔️3.13
️✔️serviceconnector
️✔️latest
️✔️3.12
️✔️3.13
️✔️servicefabric
️✔️latest
️✔️3.12
️✔️3.13
️✔️signalr
️✔️latest
️✔️3.12
️✔️3.13
️✔️sql
️✔️latest
️✔️3.12
️✔️3.13
️✔️sqlvm
️✔️latest
️✔️3.12
️✔️3.13
️✔️storage
️✔️latest
️✔️3.12
️✔️3.13
️✔️synapse
️✔️latest
️✔️3.12
️✔️3.13
️✔️telemetry
️✔️latest
️✔️3.12
️✔️3.13
️✔️util
️✔️latest
️✔️3.12
️✔️3.13
️✔️vm
️✔️latest
️✔️3.12
️✔️3.13

@azure-client-tools-bot-prd
Copy link
Copy Markdown

Hi @YangAn-microsoft,
Since the current milestone time is less than 7 days, this pr will be reviewed in the next milestone.

@azure-client-tools-bot-prd
Copy link
Copy Markdown

azure-client-tools-bot-prd Bot commented May 22, 2026

️✔️AzureCLI-BreakingChangeTest
️✔️Non Breaking Changes

@yonzhan
Copy link
Copy Markdown
Collaborator

yonzhan commented May 22, 2026

Thank you for your contribution! We will review the pull request and get back to you soon.

@github-actions
Copy link
Copy Markdown

The git hooks are available for azure-cli and azure-cli-extensions repos. They could help you run required checks before creating the PR.

Please sync the latest code with latest dev branch (for azure-cli) or main branch (for azure-cli-extensions).
After that please run the following commands to enable git hooks:

pip install azdev --upgrade
azdev setup -c <your azure-cli repo path> -r <your azure-cli-extensions repo path>

@YangAn-microsoft YangAn-microsoft changed the title build(windows): enable site in python._pth instead of deleting it {packaging} enable site in python._pth instead of deleting it May 22, 2026
Comment on lines +115 to +118
REM than deleting it) because on Python 3.14+ removing it breaks pip's
REM PEP 517 isolated BuildEnvironment subprocess: the child python.exe
REM can no longer locate the stdlib zip and dies in init_fs_encoding
REM with ModuleNotFoundError: No module named 'encodings'.
Copy link
Copy Markdown
Member

@jiasli jiasli May 22, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you find evidence that Python 3.14 specifically changed the behavior when _pth file is deleted?

The del python*._pth logic was there since long ago as a manual process and was added to build.cmd in #21746 (comment).

Copy link
Copy Markdown
Contributor Author

@YangAn-microsoft YangAn-microsoft May 22, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No there isn't a specific change introduced by Python 3.14 that caused the issue. In fact, only Python 3.14 win32 embedded has trouble importing from python*.zip.
I came up with this fix after experimenting the official way of enabling site packages for embedded python. (https://docs.python.org/3/using/windows.html#finding-modules)
Since this solves the win32 issue and using a more officially recommended solution than existing del python*._pth, I believe it's a proper change.

@YangAn-microsoft YangAn-microsoft requested a review from jiasli May 22, 2026 04:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants