Repository: ESMValGroup/ESMValCore

Issue:
Use sys.executable to find python interpreter to run diagnostic
At the moment, we use the command `python` to run a diagnostic script. It would be more reliable to try to use the same interpreter (using ` sys.executable`) as the `esmvaltool` command. This would then also work even when the conda environment in which esmvaltool is installed is not activated.

Discussion:

Patch:
diff --git a/esmvalcore/_task.py b/esmvalcore/_task.py
index de36e3d34..49682c0cc 100644
--- a/esmvalcore/_task.py
+++ b/esmvalcore/_task.py
@@ -8,6 +8,7 @@ import numbers
 import os
 import pprint
 import subprocess
+import sys
 import threading
 import time
 from copy import deepcopy
@@ -311,7 +312,10 @@ class DiagnosticTask(BaseTask):
                 raise DiagnosticError(
                     f"{err_msg}: non-executable file with unknown extension "
                     f"'{script_file.suffix}'.")
-            interpreter = which(interpreters[ext])
+            if ext == 'py' and sys.executable:
+                interpreter = sys.executable
+            else:
+                interpreter = which(interpreters[ext])
             if interpreter is None:
                 raise DiagnosticError(
                     f"{err_msg}: program '{interpreters[ext]}' not installed.")


Test patch:
diff --git a/tests/unit/task/test_diagnostic_task.py b/tests/unit/task/test_diagnostic_task.py
index e412985da..823e9a580 100644
--- a/tests/unit/task/test_diagnostic_task.py
+++ b/tests/unit/task/test_diagnostic_task.py
@@ -78,6 +78,7 @@ def test_initialize_cmd(ext_profile, cmd, tmp_path, monkeypatch):
     }
 
     monkeypatch.setattr(esmvalcore._task, 'which', lambda x: x)
+    monkeypatch.setattr(esmvalcore._task.sys, 'executable', 'python')
 
     task = esmvalcore._task.DiagnosticTask(script,
                                            settings,