From 3b562f92f5a30765ceca2659f02eb6abb7c610d3 Mon Sep 17 00:00:00 2001 From: Orgad Shaneh Date: Mon, 10 Feb 2025 22:47:36 +0200 Subject: [PATCH] {Packaging} Fix crash when another Python is installed on the system The scripts are installed in /Scripts, but PYTHONPATH was set to /src, although this directory doesn't even exist. I guess the deployment used to be in src, but this is no longer the case. 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). Fixes #28004, #29630, #30517, #30536, #30594, #30700, #30792. --- build_scripts/windows/scripts/az | 4 +++- build_scripts/windows/scripts/az_msi.cmd | 1 + build_scripts/windows/scripts/az_zip.cmd | 1 + src/azure-cli/az | 2 +- src/azure-cli/az.bat | 6 +++--- 5 files changed, 9 insertions(+), 5 deletions(-) diff --git a/build_scripts/windows/scripts/az b/build_scripts/windows/scripts/az index 6600548847e..d87c2401e90 100644 --- a/build_scripts/windows/scripts/az +++ b/build_scripts/windows/scripts/az @@ -1,3 +1,5 @@ #!/usr/bin/env bash -AZ_INSTALLER=MSI "$(dirname "${BASH_SOURCE[0]}")/../python.exe" -IBm azure.cli "$@" +SCRIPT_DIR=$(dirname "${BASH_SOURCE[0]}") +export PYTHONPATH="$(dirname "$SCRIPT_DIR")" +AZ_INSTALLER=MSI "$SCRIPT_DIR/../python.exe" -IBm azure.cli "$@" diff --git a/build_scripts/windows/scripts/az_msi.cmd b/build_scripts/windows/scripts/az_msi.cmd index 9e9708908c6..feb1549be65 100644 --- a/build_scripts/windows/scripts/az_msi.cmd +++ b/build_scripts/windows/scripts/az_msi.cmd @@ -4,6 +4,7 @@ :: @IF EXIST "%~dp0\..\python.exe" ( + SET PYTHONPATH=%~dp0\..;%PYTHONPATH% SET AZ_INSTALLER=MSI "%~dp0\..\python.exe" -IBm azure.cli %* ) ELSE ( diff --git a/build_scripts/windows/scripts/az_zip.cmd b/build_scripts/windows/scripts/az_zip.cmd index 07e4acb73ff..bdfec6953c2 100644 --- a/build_scripts/windows/scripts/az_zip.cmd +++ b/build_scripts/windows/scripts/az_zip.cmd @@ -4,6 +4,7 @@ :: @IF EXIST "%~dp0\..\python.exe" ( + SET PYTHONPATH=%~dp0\..;%PYTHONPATH% SET AZ_INSTALLER=ZIP "%~dp0\..\python.exe" -IBm azure.cli %* ) ELSE ( diff --git a/src/azure-cli/az b/src/azure-cli/az index 96ee4dc639f..a157abcae3b 100644 --- a/src/azure-cli/az +++ b/src/azure-cli/az @@ -3,7 +3,7 @@ import sys import os -dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'src') +dir = os.path.dirname(os.path.dirname(os.path.realpath(__file__))) if os.environ.get('PYTHONPATH') is None: os.environ['PYTHONPATH'] = dir diff --git a/src/azure-cli/az.bat b/src/azure-cli/az.bat index c16942a2e42..c85fab080a2 100644 --- a/src/azure-cli/az.bat +++ b/src/azure-cli/az.bat @@ -1,11 +1,11 @@ @echo off setlocal -SET PYTHONPATH=%~dp0\src;%PYTHONPATH% +SET PYTHONPATH=%~dp0\..;%PYTHONPATH% SET AZ_INSTALLER=PIP -IF EXIST "%~dp0\python.exe" ( - "%~dp0\python.exe" -m azure.cli %* +IF EXIST "%~dp0\..\python.exe" ( + "%~dp0\..\python.exe" -m azure.cli %* ) ELSE ( python -m azure.cli %* )