You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@spark.apache.org by ru...@apache.org on 2026/03/30 11:29:59 UTC

(spark) branch master updated: [SPARK-56270][PYTHON] Fix type hints and import issue for find_spark_home

This is an automated email from the ASF dual-hosted git repository.

ruifengz pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/spark.git


The following commit(s) were added to refs/heads/master by this push:
     new 8daa5332f065 [SPARK-56270][PYTHON] Fix type hints and import issue for find_spark_home
8daa5332f065 is described below

commit 8daa5332f0653b4953b67688e3040a9d4310a235
Author: Tian Gao <ga...@hotmail.com>
AuthorDate: Mon Mar 30 19:29:40 2026 +0800

    [SPARK-56270][PYTHON] Fix type hints and import issue for find_spark_home
    
    ### What changes were proposed in this pull request?
    
    * Remove the type hint ignore for `find_spark_home` module and fill in the type hints
    * Remove the `ImportError` check and `import_error_raised` related logic
    
    ### Why are the changes needed?
    
    There's no reason we can't type hint this module so I just did it.
    
    For the import part, it's impossible to trigger that path because `find_spec("pyspark")` will never trigger `ImportError`. It either returns a spec or `None`. I searched the history and the first time it was introduced was in https://github.com/apache/spark/pull/15659 . There were 2 branches and the one for python2 actually will trigger the `ImportError`. The python3 branch won't work but it might just be ignored.
    
    In https://github.com/apache/spark/pull/28152 we added `import_error_raised` check and extra messages, but we only tested in python2, which would trigger that message. I confirmed that the message won't be printed if we can't find `pyspark`.
    
    Now we print the message if we can't find `SPARK_HOME`.
    
    ### Does this PR introduce _any_ user-facing change?
    
    No.
    
    ### How was this patch tested?
    
    Locally confirmed the message will be printed if `pyspark` can't be imported. Lint also passed locally.
    
    ### Was this patch authored or co-authored using generative AI tooling?
    
    No.
    
    Closes #55067 from gaogaotiantian/fix-find-spark-home-type-hint.
    
    Authored-by: Tian Gao <ga...@hotmail.com>
    Signed-off-by: Ruifeng Zheng <ru...@apache.org>
---
 python/mypy.ini                   |  3 ---
 python/pyspark/find_spark_home.py | 42 ++++++++++++++++++---------------------
 2 files changed, 19 insertions(+), 26 deletions(-)

diff --git a/python/mypy.ini b/python/mypy.ini
index f0bf27b4b85b..b2d49fdf92a7 100644
--- a/python/mypy.ini
+++ b/python/mypy.ini
@@ -32,9 +32,6 @@ ignore_errors = True
 [mypy-pyspark.daemon]
 disallow_untyped_defs = False
 
-[mypy-pyspark.find_spark_home]
-disallow_untyped_defs = False
-
 [mypy-pyspark._globals]
 disallow_untyped_defs = False
 
diff --git a/python/pyspark/find_spark_home.py b/python/pyspark/find_spark_home.py
index a2226f8385ef..e572033ba6e3 100755
--- a/python/pyspark/find_spark_home.py
+++ b/python/pyspark/find_spark_home.py
@@ -24,13 +24,13 @@ import os
 import sys
 
 
-def _find_spark_home():
+def _find_spark_home() -> str:
     """Find the SPARK_HOME."""
     # If the environment has SPARK_HOME set trust it.
     if "SPARK_HOME" in os.environ:
         return os.environ["SPARK_HOME"]
 
-    def is_spark_home(path):
+    def is_spark_home(path: str) -> bool:
         """Takes a path and returns true if the provided path could be a reasonable SPARK_HOME"""
         return os.path.isfile(os.path.join(path, "bin/spark-submit")) and (
             os.path.isdir(os.path.join(path, "jars"))
@@ -52,19 +52,16 @@ def _find_spark_home():
         ]
 
     # Add the path of the PySpark module if it exists
-    import_error_raised = False
     from importlib.util import find_spec
 
-    try:
-        module_home = os.path.dirname(find_spec("pyspark").origin)
+    spec = find_spec("pyspark")
+    if spec is not None and spec.origin is not None:
+        module_home = os.path.dirname(spec.origin)
         paths.append(os.path.join(module_home, spark_dist_dir))
         paths.append(module_home)
         # If we are installed in edit mode also look two dirs up
         # Downloading different versions are not supported in edit mode.
         paths.append(os.path.join(module_home, "../../"))
-    except ImportError:
-        # Not pip installed no worries
-        import_error_raised = True
 
     # Normalize the paths
     paths = [os.path.abspath(p) for p in paths]
@@ -73,21 +70,20 @@ def _find_spark_home():
         return next(path for path in paths if is_spark_home(path))
     except StopIteration:
         print("Could not find valid SPARK_HOME while searching {0}".format(paths), file=sys.stderr)
-        if import_error_raised:
-            print(
-                "\nDid you install PySpark via a package manager such as pip or Conda? If so,\n"
-                "PySpark was not found in your Python environment. It is possible your\n"
-                "Python environment does not properly bind with your package manager.\n"
-                "\nPlease check your default 'python' and if you set PYSPARK_PYTHON and/or\n"
-                "PYSPARK_DRIVER_PYTHON environment variables, and see if you can import\n"
-                "PySpark, for example, 'python -c 'import pyspark'.\n"
-                "\nIf you cannot import, you can install by using the Python executable directly,\n"
-                "for example, 'python -m pip install pyspark [--user]'. Otherwise, you can also\n"
-                "explicitly set the Python executable, that has PySpark installed, to\n"
-                "PYSPARK_PYTHON or PYSPARK_DRIVER_PYTHON environment variables, for example,\n"
-                "'PYSPARK_PYTHON=python3 pyspark'.\n",
-                file=sys.stderr,
-            )
+        print(
+            "\nDid you install PySpark via a package manager such as pip or Conda? If so,\n"
+            "PySpark was not found in your Python environment. It is possible your\n"
+            "Python environment does not properly bind with your package manager.\n"
+            "\nPlease check your default 'python' and if you set PYSPARK_PYTHON and/or\n"
+            "PYSPARK_DRIVER_PYTHON environment variables, and see if you can import\n"
+            "PySpark, for example, 'python -c 'import pyspark'.\n"
+            "\nIf you cannot import, you can install by using the Python executable directly,\n"
+            "for example, 'python -m pip install pyspark [--user]'. Otherwise, you can also\n"
+            "explicitly set the Python executable, that has PySpark installed, to\n"
+            "PYSPARK_PYTHON or PYSPARK_DRIVER_PYTHON environment variables, for example,\n"
+            "'PYSPARK_PYTHON=python3 pyspark'.\n",
+            file=sys.stderr,
+        )
         sys.exit(-1)
 
 


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@spark.apache.org
For additional commands, e-mail: commits-help@spark.apache.org