@@ -504,7 +504,7 @@ object Run extends ScalaCommand[RunOptions] with BuildCommandHelpers {
504504 val (pythonExecutable, pythonLibraryPaths, pythonExtraEnv) =
505505 if (setupPython) {
506506 val (exec, libPaths) = value {
507- val python = Python ( )
507+ val python = value(createPythonInstance().orPythonDetectionError )
508508 val pythonPropertiesOrError = for {
509509 paths <- python.nativeLibraryPaths
510510 executable <- python.executable
@@ -586,7 +586,7 @@ object Run extends ScalaCommand[RunOptions] with BuildCommandHelpers {
586586 val (pythonJavaProps, pythonExtraEnv) =
587587 if (setupPython) {
588588 val scalapyProps = value {
589- val python = Python ( )
589+ val python = value(createPythonInstance().orPythonDetectionError )
590590 val propsOrError = python.scalapyProperties
591591 logger.debug(s " Python Java properties: $propsOrError" )
592592 propsOrError.orPythonDetectionError
@@ -712,8 +712,64 @@ object Run extends ScalaCommand[RunOptions] with BuildCommandHelpers {
712712 logger = logger
713713 ).map(f)
714714
715+ def findHomebrewPython (): Option [os.Path ] =
716+ if (Properties .isMac) {
717+ // Try common Homebrew locations
718+ val homebrewPaths = Seq (
719+ " /opt/homebrew/bin/python3" ,
720+ " /usr/local/bin/python3"
721+ )
722+ homebrewPaths
723+ .map(os.Path (_, os.pwd))
724+ .find { path =>
725+ os.exists(path) && {
726+ // Verify it has the -config script
727+ val configPath = path / os.up / s " ${path.last}-config "
728+ os.exists(configPath)
729+ }
730+ }
731+ }
732+ else None
733+
734+ def createPythonInstance (): Try [Python ] =
735+ // Try default Python detection first
736+ // If it fails on macOS and Homebrew Python is available, the error message will guide the user
737+ Try (Python ())
738+
715739 final class PythonDetectionError (cause : Throwable ) extends BuildException (
716- s " Error detecting Python environment: ${cause.getMessage}" ,
740+ {
741+ val baseMessage = cause.getMessage
742+ if (
743+ Properties .isMac && baseMessage != null && baseMessage.contains(
744+ " -config"
745+ ) && baseMessage.contains(" does not exist" )
746+ ) {
747+ val homebrewPython = findHomebrewPython()
748+ val homebrewHint = homebrewPython match {
749+ case Some (path) =>
750+ s """
751+ |Homebrew Python was found at $path, but it's not being used.
752+ |Ensure Homebrew's bin directory is first in your PATH:
753+ | export PATH=" ${path / os.up}: $$ PATH"
754+ | """ .stripMargin
755+ case None =>
756+ """
757+ |Consider installing Python via Homebrew:
758+ | brew install python
759+ |
760+ |Or install Python from https://www.python.org/downloads/
761+ |""" .stripMargin
762+ }
763+ s """ Error detecting Python environment: $baseMessage
764+ |
765+ |The system Python from CommandLineTools may not include the required -config scripts.
766+ | $homebrewHint
767+ |
768+ |Alternatively, you can disable Python setup with --python=false """ .stripMargin
769+ }
770+ else
771+ s " Error detecting Python environment: $baseMessage"
772+ },
717773 cause = cause
718774 )
719775
0 commit comments