Skip to content

{Packaging} Prepend embedded Python folder to PYTHONPATH on Windows#30801

Open
orgads wants to merge 1 commit intoAzure:devfrom
orgads:pythonpath
Open

{Packaging} Prepend embedded Python folder to PYTHONPATH on Windows#30801
orgads wants to merge 1 commit intoAzure:devfrom
orgads:pythonpath

Conversation

@orgads
Copy link
Copy Markdown
Contributor

@orgads orgads commented Feb 10, 2025

Setting PYTHONPATH is done to avoid conflicts with other installations of Python on the same machine. When it is set incorrectly, python crash on startup (typically when loading incompatible ctypes). see #30594 (comment)

Update the scripts to prepend CLI2 in PYTHONPATH.

Fix #29630, #30517, #30536, #30594, #30700, #30792.

PS: In src/azure-cli/az and src/azure-cli/az.bat, src is added in PYTHONPATH. It seems to be a mistake. But these scripts no longer seem to be used.


This checklist is used to make sure that common guidelines for a pull request are followed.

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

azure-client-tools-bot-prd bot commented Feb 10, 2025

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

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

azure-client-tools-bot-prd bot commented Feb 10, 2025

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

@yonzhan
Copy link
Copy Markdown
Collaborator

yonzhan commented Feb 10, 2025

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>

@microsoft-github-policy-service
Copy link
Copy Markdown
Contributor

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

@microsoft-github-policy-service microsoft-github-policy-service bot added customer-reported Issues that are reported by GitHub users external to the Azure organization. Auto-Assign Auto assign by bot labels Feb 10, 2025
@bebound
Copy link
Copy Markdown
Contributor

bebound commented Feb 12, 2025

At a glance, I have no idea why it adds src folder (os.path.join(os.path.dirname(os.path.realpath(__file__)), 'src') and %~dp0\src) to PYTHONPATH. The src folder does not exist. Since these two files are very old, maybe something has changed as time goes by. @jiasli may have some context.

However, this won't fix the issue. az.cmd is the entrypoint on Windows.

@orgads
Copy link
Copy Markdown
Contributor Author

orgads commented Feb 12, 2025

At a glance, I have no idea why it adds src folder (os.path.join(os.path.dirname(os.path.realpath(__file__)), 'src') and %~dp0\src) to PYTHONPATH. The src folder does not exist. Since these two files are very old, maybe something has changed as time goes by. @jiasli may have some context.

However, this won't fix the issue. az.cmd is the entrypoint on Windows.

Oops, you're right.

Fixed and tested this time - both on bash and cmd.

@orgads
Copy link
Copy Markdown
Contributor Author

orgads commented Feb 12, 2025

Updated the commit message and the PR description accordingly.

@orgads
Copy link
Copy Markdown
Contributor Author

orgads commented Feb 12, 2025

I guess ps1 script should also be updated, but it is signed, so I can't edit it.

You can add this in the first line:

$env:PYTHONPATH="$PSScriptRoot\..;$env:PYTHONPATH"

@bebound
Copy link
Copy Markdown
Contributor

bebound commented Feb 14, 2025

What's the output of this in your computer, and what's the version of the extra Python?

cd "C:\Program Files\Microsoft SDKs\Azure\CLI2"
./python.exe -m site 

I guess bumping it to 3.12.8 can also fix the crash.

@orgads
Copy link
Copy Markdown
Contributor Author

orgads commented Feb 14, 2025

sys.path = [
    'C:\\Program Files\\Microsoft SDKs\\Azure\\CLI2',
    'C:\\Program Files\\Microsoft SDKs\\Azure\\CLI2\\python312.zip',
    'C:\\Users\\orgads\\AppData\\Local\\Programs\\Python\\Python312\\Lib',
    'C:\\Users\\orgads\\AppData\\Local\\Programs\\Python\\Python312\\DLLs',
    'C:\\Program Files\\Microsoft SDKs\\Azure\\CLI2\\Lib',
    'C:\\Program Files\\Microsoft SDKs\\Azure\\CLI2\\Lib\\site-packages',
    'C:\\Program Files\\Microsoft SDKs\\Azure\\CLI2\\Lib\\site-packages\\win32',
    'C:\\Program Files\\Microsoft SDKs\\Azure\\CLI2\\Lib\\site-packages\\win32\\lib',
    'C:\\Program Files\\Microsoft SDKs\\Azure\\CLI2\\Lib\\site-packages\\Pythonwin',
]
USER_BASE: 'C:\\Users\\orgads\\AppData\\Roaming\\Python' (exists)
USER_SITE: 'C:\\Users\\orgads\\AppData\\Roaming\\Python\\Python312\\site-packages' (doesn't exist)
ENABLE_USER_SITE: True

My local Python is 3.12.6.

There shouldn't be any dependency between the bundled python and system python. You cannot expect users to follow which python version you bundle and upgrade their local python accordingly. Do you see anything wrong with the suggested fix?

@bebound
Copy link
Copy Markdown
Contributor

bebound commented Feb 17, 2025

I just want to confirm that the root cause is the compatibility issue between 3.12.x and 3.12.8.

I have concern about the changes in src/azure-cli/az and src/azure-cli/az.bat. AFAIS, the PYTHONPATH statement can be removed. It just added an invalid path for years.

Here is a related issue: pypa/pip#13023. There is no easy way to let pip ignore C:\\Users\\xxx\\AppData\\Local\\Programs\\Python\\Python312\\Lib.

@IF EXIST "%~dp0\..\python.exe" (
SET PYTHONPATH=%~dp0\..;%PYTHONPATH%
SET AZ_INSTALLER=ZIP
"%~dp0\..\python.exe" -IBm azure.cli %*
Copy link
Copy Markdown
Contributor

@bebound bebound Feb 17, 2025

Choose a reason for hiding this comment

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

The root cause of the crash is Python 3.12.8 loads 3.12.x's module(C:\\Users\\xxx\\AppData\\Local\\Programs\\Python\\Python312\\Lib) when run pip.(#30594 (comment))

We have -I option when run az, but we don't have it in pip.

cmd = [sys.executable, '-m', 'pip'] + pip_exec_args + ['--disable-pip-version-check', '--no-cache-dir']

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Is this just an observation, or do you want me to change anything in my commit?

@bebound
Copy link
Copy Markdown
Contributor

bebound commented Feb 17, 2025

This PR may also fix the windows az extension add issue: #25067 (comment) The windows pip compile package issue won't be fixed by this PR. It seems pip ignore PYTHONPAHT in build env. ./python.exe -m pip install pendulum --no-binary :all: still fails with ModuleNotFoundError: No module named '_socket' after adding PYTHONPATH.

This PR fixes the pip crash issue when user installed another python, and pip loads the wrong module from user's installed python. See #30594 (comment)
I don't know why python tries to load # extension module '_ctypes' loaded from 'C:\\Users\\hanglei\\AppData\\Local\\Programs\\Python\\Python312\\DLLs\\_ctypes.pyd' instead of C:\\Program Files\\Microsoft SDKs\\Azure\\CLI2\\_ctypes.pyd', though C:\\Program Files\\Microsoft SDKs\\Azure\\CLI2 has higher priority in sys.path.

@bebound bebound changed the title {Packaging} Fix crash when another Python is installed on the system {Packaging} Add embedded Python folder to PYTHONPATH on Windows Feb 17, 2025
Setting PYTHONPATH is done to avoid conflicts with other installations
of Python on the same machine. When it is set incorrectly Python tends
to crash on startup (typically when loading incompatible ctypes).

There were old scripts in <base>/Scripts, which set PYTHONPATH so that
Python prefers the bundled libs over the system ones. The problem with
these scripts is that they set the wrong path.

But these scripts no longer seem to be used. They were replaced by new
ones in wbin. The problem with those is that PYTHONPATH was not set at
all in them.

Update the scripts to set PYTHONPATH correctly, both for bash and cmd.

Fixes Azure#25067, Azure#28004, Azure#29630, Azure#30517, Azure#30536, Azure#30594, Azure#30700, Azure#30792.
@bebound bebound changed the title {Packaging} Add embedded Python folder to PYTHONPATH on Windows {Packaging} Prepend embedded Python folder to PYTHONPATH on Windows Feb 18, 2025
@orgads
Copy link
Copy Markdown
Contributor Author

orgads commented Mar 16, 2025

ping?

@bebound
Copy link
Copy Markdown
Contributor

bebound commented Mar 17, 2025

I'm able to repro this by installing Python 3.12.6.

I initially liked this solution because it might also fix the Windows pip compile package issue. However, after checking again, it doesn't resolve the problem.
Adding the -I option when calling pip is a more direct solution.

@jiasli to share more insight.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

act-platform-engineering-squad Auto-Assign Auto assign by bot customer-reported Issues that are reported by GitHub users external to the Azure organization. Packaging

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Impossible to install any extensions

4 participants