Skip to content

Commit

Permalink
{Packaging} Fix crash when another Python is installed on the system
Browse files Browse the repository at this point in the history
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 #28004, #29630, #30517, #30536, #30594, #30700, #30792.
  • Loading branch information
orgads committed Feb 12, 2025
1 parent e1a34cc commit bd170f7
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 5 deletions.
4 changes: 3 additions & 1 deletion build_scripts/windows/scripts/az
Original file line number Diff line number Diff line change
@@ -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 "$@"
1 change: 1 addition & 0 deletions build_scripts/windows/scripts/az_msi.cmd
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
::

@IF EXIST "%~dp0\..\python.exe" (
SET PYTHONPATH=%~dp0\..;%PYTHONPATH%
SET AZ_INSTALLER=MSI
"%~dp0\..\python.exe" -IBm azure.cli %*
) ELSE (
Expand Down
1 change: 1 addition & 0 deletions build_scripts/windows/scripts/az_zip.cmd
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
::

@IF EXIST "%~dp0\..\python.exe" (
SET PYTHONPATH=%~dp0\..;%PYTHONPATH%
SET AZ_INSTALLER=ZIP
"%~dp0\..\python.exe" -IBm azure.cli %*
) ELSE (
Expand Down
2 changes: 1 addition & 1 deletion src/azure-cli/az
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions src/azure-cli/az.bat
Original file line number Diff line number Diff line change
@@ -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 %*
)

0 comments on commit bd170f7

Please # to comment.