Skip to content

[SPARK-52669][PySpark]Improvement PySpark choose pythonExec in cluster yarn client mode #51357

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion python/pyspark/core/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ def _do_init(
)
os.environ["SPARK_BUFFER_SIZE"] = str(self._jvm.PythonUtils.getSparkBufferSize(self._jsc))

self.pythonExec = os.environ.get("PYSPARK_PYTHON", "python3")
self.pythonExec = os.environ.get("PYSPARK_PYTHON", self._get_python_exec_from_conf())
self.pythonVer = "%d.%d" % sys.version_info[:2]

# Broadcast's __reduce__ method stores Broadcast instances here.
Expand Down Expand Up @@ -414,6 +414,30 @@ def signal_handler(signal: Any, frame: Any) -> NoReturn:
):
signal.signal(signal.SIGINT, signal_handler)

def _get_python_exec_from_conf(self) -> str:
"""
Used to refine the value of the pythonExec variable serialized by the driver in client mode when PYSPARK_PYTHON is not configured.
If the environment variable PYSPARK_PYTHON is not set, this method will first try to find the Python environment from the Spark configuration object.
If no valid configuration is found, it will return the default Python executable 'python3'.

Returns:
str: The path to the Python executable.
"""
# List of configuration keys to check for Python executable path
config_keys = [
"spark.pyspark.driver.python",
"spark.pyspark.python",
"spark.executorEnv.PYSPARK_DRIVER_PYTHON",
"spark.executorEnv.PYSPARK_PYTHON"
]
python_exec = "python3"
for key in config_keys:
python_exec = self._conf.get(key, None)
if python_exec is not None and python_exec.strip() != "":
python_exec = python_exec.strip()
break
return python_exec

def __repr__(self) -> str:
return "<SparkContext master={master} appName={appName}>".format(
master=self.master,
Expand Down