[SPARK-52669][PYSPARK] Fix Python executable selection for YARN client mode #51357#55310
Open
gwdgithubnom wants to merge 1 commit intoapache:masterfrom
Open
[SPARK-52669][PYSPARK] Fix Python executable selection for YARN client mode #51357#55310gwdgithubnom wants to merge 1 commit intoapache:masterfrom
gwdgithubnom wants to merge 1 commit intoapache:masterfrom
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What changes were proposed in this pull request?
This PR improves the Python executable selection logic in
SparkContextto resolve version mismatch issues, particularly in YARN client mode.Previously, the driver might fail to locate the correct Python interpreter when
PYSPARK_PYTHONwas not explicitly set in the shell environment, even if it was defined inSparkConf. This led toRuntimeErrordue to minor version discrepancies between the driver and executors (e.g., Driver using system Python 3.10 while Executors use archived Python 3.6).Key changes:
_get_python_exec_from_confmethod that follows a 7-level priority sequence to ensure consistency:PYSPARK_DRIVER_PYTHON(Env) >PYSPARK_PYTHON(Env) >spark.pyspark.driver.python(Conf) >spark.pyspark.python(Conf) >spark.executorEnv.PYSPARK_DRIVER_PYTHON>spark.executorEnv.PYSPARK_PYTHON> Default (python3)../environment/bin/python) via Spark configuration without requiring manual environment variable exports for every script execution.ruff,mypy).Note: This PR is a revival and optimization of #51357.
Why are the changes needed?
PySpark requires the driver and executors to use consistent Python minor versions. In many production environments (especially when using conda-pack or virtualenvs),
PYSPARK_PYTHONis passed viaSparkConfrather than system-wide environment variables.Without this fix, the driver falls back to the system default Python when scripts are launched directly, causing a mismatch with the executor's archived Python environment. This change automates the resolution, making the deployment more robust and user-friendly by eliminating the need to manually export environment variables for each session.
Does this PR introduce any user-facing change?
No.
How was this patch tested?
sys.versionandsys.executablematch between the driver and executors using:pyspark/tests/test_context.pyto verify the 7-level priority logic and ensure correct overrides between environment variables and Spark configurations.dev/lint-pythonchecks.Was this patch authored or co-authored using generative AI tooling?
No.