Skip to content

Commit 1ea18c8

Browse files
authored
Make run --python work inside virtualenv for Scala Native (#1399)
1 parent 4bea696 commit 1ea18c8

File tree

1 file changed

+13
-7
lines changed
  • modules/cli/src/main/scala/scala/cli/commands

1 file changed

+13
-7
lines changed

modules/cli/src/main/scala/scala/cli/commands/Run.scala

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -365,19 +365,22 @@ object Run extends ScalaCommand[RunOptions] with BuildCommandHelpers {
365365
value(res)
366366
case Platform.Native =>
367367
val setupPython = build.options.notForBloopOptions.doSetupPython.getOrElse(false)
368-
val pythonLibraryPaths =
368+
val (pythonExecutable, pythonLibraryPaths) =
369369
if (setupPython)
370370
value {
371-
val python = Python()
372-
val pathsOrError = python.nativeLibraryPaths
373-
logger.debug(s"Python native library paths: $pathsOrError")
374-
pathsOrError.orPythonDetectionError
371+
val python = Python()
372+
val pythonPropertiesOrError = for {
373+
paths <- python.nativeLibraryPaths
374+
executable <- python.executable
375+
} yield (Some(executable), paths)
376+
logger.debug(s"Python executable and native library paths: $pythonPropertiesOrError")
377+
pythonPropertiesOrError.orPythonDetectionError
375378
}
376379
else
377-
Nil
380+
(None, Nil)
378381
// seems conda doesn't add the lib directory to LD_LIBRARY_PATH (see conda/conda#308),
379382
// which prevents apps from finding libpython for example, so we update it manually here
380-
val extraEnv =
383+
val libraryPathsEnv =
381384
if (pythonLibraryPaths.isEmpty) Map.empty
382385
else {
383386
val prependTo =
@@ -397,6 +400,9 @@ object Run extends ScalaCommand[RunOptions] with BuildCommandHelpers {
397400
Map(prependTo -> newValue)
398401
}
399402
}
403+
val programNameEnv =
404+
pythonExecutable.fold(Map.empty)(py => Map("SCALAPY_PYTHON_PROGRAMNAME" -> py))
405+
val extraEnv = libraryPathsEnv ++ programNameEnv
400406
val maybeResult = withNativeLauncher(
401407
build,
402408
mainClass,

0 commit comments

Comments
 (0)