changeset 31131:6cfc7af8eacc

python.m: Add code to find python executable on different platforms (bug #62733) * python.m: New persistent variable "pyexec" to hold name of python executable. * python.m (get_python_executable): New function to find name of python executable.
author Rik <rik@octave.org>
date Sat, 09 Jul 2022 10:22:57 -0700
parents 0c637fa9529a
children 0f4d16af143b
files scripts/miscellaneous/python.m
diffstat 1 files changed, 18 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/miscellaneous/python.m	Sat Jul 09 09:58:02 2022 -0700
+++ b/scripts/miscellaneous/python.m	Sat Jul 09 10:22:57 2022 -0700
@@ -38,6 +38,8 @@
 
 function [output, status] = python (scriptfile = "-c ''", varargin)
 
+  persistent pyexec = get_python_executable ();
+
   if (ischar (scriptfile)
       && (   (nargin == 1 && ! isempty (scriptfile))
           || (nargin != 1 && iscellstr (varargin))))
@@ -46,7 +48,7 @@
       scriptfile = file_in_loadpath (scriptfile);
     endif
 
-    [status, output] = system (["python ", scriptfile, ...
+    [status, output] = system ([pyexec " " scriptfile, ...
                                 sprintf(" %s", varargin{:})]);
   else
     error ("python: invalid arguments");
@@ -54,5 +56,20 @@
 
 endfunction
 
+function pyexec = get_python_executable () 
+
+  pyexec = getenv ("PYTHON");
+  if (isempty (pyexec))
+    ## PEP394 says Python 3 installs should all provide this command
+    pyexec = "python3";
+
+    if (ispc () && (! isunix ()))
+      ## 2020-03: Python.org installer/Anaconda do not provide python3
+      pyexec = "python";
+    endif
+  endif
+
+endfunction
+
 
 %!error <invalid arguments> python (123)