Mercurial > jwe > octave
annotate scripts/miscellaneous/python.m @ 31144:22305b923761
doc: which Python executable and how to change it (bug #62733)
* python.m: Document difference between Unix and Windows. Fix whitespace.
author | Colin Macdonald <cbm@m.fsf.org> |
---|---|
date | Sat, 09 Jul 2022 22:05:40 -0700 |
parents | 0f4d16af143b |
children |
rev | line source |
---|---|
27928
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27924
diff
changeset
|
1 ######################################################################## |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27924
diff
changeset
|
2 ## |
30569
796f54d4ddbf
update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents:
29363
diff
changeset
|
3 ## Copyright (C) 2008-2022 The Octave Project Developers |
27923
b442ec6dda5c
use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents:
26379
diff
changeset
|
4 ## |
27928
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27924
diff
changeset
|
5 ## See the file COPYRIGHT.md in the top-level directory of this |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27924
diff
changeset
|
6 ## distribution or <https://octave.org/copyright/>. |
13163
2ca9730d35ba
python: new function to invoke python scripts from octave code
Carnë Draug <carandraug+dev@gmail.com>
parents:
diff
changeset
|
7 ## |
2ca9730d35ba
python: new function to invoke python scripts from octave code
Carnë Draug <carandraug+dev@gmail.com>
parents:
diff
changeset
|
8 ## This file is part of Octave. |
2ca9730d35ba
python: new function to invoke python scripts from octave code
Carnë Draug <carandraug+dev@gmail.com>
parents:
diff
changeset
|
9 ## |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23220
diff
changeset
|
10 ## Octave is free software: you can redistribute it and/or modify it |
13163
2ca9730d35ba
python: new function to invoke python scripts from octave code
Carnë Draug <carandraug+dev@gmail.com>
parents:
diff
changeset
|
11 ## under the terms of the GNU General Public License as published by |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23220
diff
changeset
|
12 ## the Free Software Foundation, either version 3 of the License, or |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
13 ## (at your option) any later version. |
13163
2ca9730d35ba
python: new function to invoke python scripts from octave code
Carnë Draug <carandraug+dev@gmail.com>
parents:
diff
changeset
|
14 ## |
2ca9730d35ba
python: new function to invoke python scripts from octave code
Carnë Draug <carandraug+dev@gmail.com>
parents:
diff
changeset
|
15 ## Octave is distributed in the hope that it will be useful, but |
2ca9730d35ba
python: new function to invoke python scripts from octave code
Carnë Draug <carandraug+dev@gmail.com>
parents:
diff
changeset
|
16 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
17 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
18 ## GNU General Public License for more details. |
13163
2ca9730d35ba
python: new function to invoke python scripts from octave code
Carnë Draug <carandraug+dev@gmail.com>
parents:
diff
changeset
|
19 ## |
2ca9730d35ba
python: new function to invoke python scripts from octave code
Carnë Draug <carandraug+dev@gmail.com>
parents:
diff
changeset
|
20 ## You should have received a copy of the GNU General Public License |
2ca9730d35ba
python: new function to invoke python scripts from octave code
Carnë Draug <carandraug+dev@gmail.com>
parents:
diff
changeset
|
21 ## along with Octave; see the file COPYING. If not, see |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23220
diff
changeset
|
22 ## <https://www.gnu.org/licenses/>. |
27928
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27924
diff
changeset
|
23 ## |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27924
diff
changeset
|
24 ######################################################################## |
13163
2ca9730d35ba
python: new function to invoke python scripts from octave code
Carnë Draug <carandraug+dev@gmail.com>
parents:
diff
changeset
|
25 |
2ca9730d35ba
python: new function to invoke python scripts from octave code
Carnë Draug <carandraug+dev@gmail.com>
parents:
diff
changeset
|
26 ## -*- texinfo -*- |
20852
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
20713
diff
changeset
|
27 ## @deftypefn {} {@var{output} =} python (@var{scriptfile}) |
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
20713
diff
changeset
|
28 ## @deftypefnx {} {@var{output} =} python (@var{scriptfile}, @var{argument1}, @var{argument2}, @dots{}) |
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
20713
diff
changeset
|
29 ## @deftypefnx {} {[@var{output}, @var{status}] =} python (@dots{}) |
14818
c6ae30f73946
Look for perl/python scripts in Octave load path (bug #36729)
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
30 ## Invoke Python script @var{scriptfile}, possibly with a list of command line |
19215
c61deba773ab
Add BIST test to perl() and python().
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
31 ## arguments. |
c61deba773ab
Add BIST test to perl() and python().
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
32 ## |
c61deba773ab
Add BIST test to perl() and python().
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
33 ## Return output in @var{output} and optional status in @var{status}. If |
20713
2469d78a1d8b
Consistently use 'filename' rather than 'file name' throughout code base.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
34 ## @var{scriptfile} is not an absolute filename it is searched for in the |
19215
c61deba773ab
Add BIST test to perl() and python().
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
35 ## current directory and then in the Octave loadpath. |
31144
22305b923761
doc: which Python executable and how to change it (bug #62733)
Colin Macdonald <cbm@m.fsf.org>
parents:
31137
diff
changeset
|
36 ## |
22305b923761
doc: which Python executable and how to change it (bug #62733)
Colin Macdonald <cbm@m.fsf.org>
parents:
31137
diff
changeset
|
37 ## Programming Note: On UNIX systems, the script will be executed by |
22305b923761
doc: which Python executable and how to change it (bug #62733)
Colin Macdonald <cbm@m.fsf.org>
parents:
31137
diff
changeset
|
38 ## @command{python3} and on Windows by @command{python}. You can override |
22305b923761
doc: which Python executable and how to change it (bug #62733)
Colin Macdonald <cbm@m.fsf.org>
parents:
31137
diff
changeset
|
39 ## these defaults by setting the @env{PYTHON} environment variable, for example |
22305b923761
doc: which Python executable and how to change it (bug #62733)
Colin Macdonald <cbm@m.fsf.org>
parents:
31137
diff
changeset
|
40 ## from within Octave using @code{setenv PYTHON /usr/local/bin/python3}. |
22305b923761
doc: which Python executable and how to change it (bug #62733)
Colin Macdonald <cbm@m.fsf.org>
parents:
31137
diff
changeset
|
41 ## |
14818
c6ae30f73946
Look for perl/python scripts in Octave load path (bug #36729)
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
42 ## @seealso{system, perl} |
13163
2ca9730d35ba
python: new function to invoke python scripts from octave code
Carnë Draug <carandraug+dev@gmail.com>
parents:
diff
changeset
|
43 ## @end deftypefn |
2ca9730d35ba
python: new function to invoke python scripts from octave code
Carnë Draug <carandraug+dev@gmail.com>
parents:
diff
changeset
|
44 |
31137
0f4d16af143b
perl.m, python.m: Redo input validation.
Rik <rik@octave.org>
parents:
31136
diff
changeset
|
45 function [output, status] = python (scriptfile, varargin) |
13163
2ca9730d35ba
python: new function to invoke python scripts from octave code
Carnë Draug <carandraug+dev@gmail.com>
parents:
diff
changeset
|
46 |
31136
6cfc7af8eacc
python.m: Add code to find python executable on different platforms (bug #62733)
Rik <rik@octave.org>
parents:
30569
diff
changeset
|
47 persistent pyexec = get_python_executable (); |
6cfc7af8eacc
python.m: Add code to find python executable on different platforms (bug #62733)
Rik <rik@octave.org>
parents:
30569
diff
changeset
|
48 |
31137
0f4d16af143b
perl.m, python.m: Redo input validation.
Rik <rik@octave.org>
parents:
31136
diff
changeset
|
49 if (nargin < 1) |
0f4d16af143b
perl.m, python.m: Redo input validation.
Rik <rik@octave.org>
parents:
31136
diff
changeset
|
50 print_usage (); |
0f4d16af143b
perl.m, python.m: Redo input validation.
Rik <rik@octave.org>
parents:
31136
diff
changeset
|
51 endif |
0f4d16af143b
perl.m, python.m: Redo input validation.
Rik <rik@octave.org>
parents:
31136
diff
changeset
|
52 |
0f4d16af143b
perl.m, python.m: Redo input validation.
Rik <rik@octave.org>
parents:
31136
diff
changeset
|
53 if (! ischar (scriptfile) || isempty (scriptfile)) |
0f4d16af143b
perl.m, python.m: Redo input validation.
Rik <rik@octave.org>
parents:
31136
diff
changeset
|
54 error ("python: SCRIPTFILE must be a non-empty string"); |
0f4d16af143b
perl.m, python.m: Redo input validation.
Rik <rik@octave.org>
parents:
31136
diff
changeset
|
55 endif |
0f4d16af143b
perl.m, python.m: Redo input validation.
Rik <rik@octave.org>
parents:
31136
diff
changeset
|
56 if (nargin > 1 && ! iscellstr (varargin)) |
0f4d16af143b
perl.m, python.m: Redo input validation.
Rik <rik@octave.org>
parents:
31136
diff
changeset
|
57 error ("python: ARGUMENTS must be strings"); |
0f4d16af143b
perl.m, python.m: Redo input validation.
Rik <rik@octave.org>
parents:
31136
diff
changeset
|
58 endif |
0f4d16af143b
perl.m, python.m: Redo input validation.
Rik <rik@octave.org>
parents:
31136
diff
changeset
|
59 |
0f4d16af143b
perl.m, python.m: Redo input validation.
Rik <rik@octave.org>
parents:
31136
diff
changeset
|
60 if (numel (scriptfile) < 2 || ! strcmp (scriptfile(1:2), "-c")) |
14818
c6ae30f73946
Look for perl/python scripts in Octave load path (bug #36729)
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
61 ## Attempt to find file in loadpath. No effect for absolute filenames. |
c6ae30f73946
Look for perl/python scripts in Octave load path (bug #36729)
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
62 scriptfile = file_in_loadpath (scriptfile); |
c6ae30f73946
Look for perl/python scripts in Octave load path (bug #36729)
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
63 endif |
13163
2ca9730d35ba
python: new function to invoke python scripts from octave code
Carnë Draug <carandraug+dev@gmail.com>
parents:
diff
changeset
|
64 |
31137
0f4d16af143b
perl.m, python.m: Redo input validation.
Rik <rik@octave.org>
parents:
31136
diff
changeset
|
65 [status, output] = system ([pyexec " " scriptfile, ... |
0f4d16af143b
perl.m, python.m: Redo input validation.
Rik <rik@octave.org>
parents:
31136
diff
changeset
|
66 sprintf(" %s", varargin{:})]); |
13163
2ca9730d35ba
python: new function to invoke python scripts from octave code
Carnë Draug <carandraug+dev@gmail.com>
parents:
diff
changeset
|
67 |
2ca9730d35ba
python: new function to invoke python scripts from octave code
Carnë Draug <carandraug+dev@gmail.com>
parents:
diff
changeset
|
68 endfunction |
17338
1c89599167a6
maint: End m-files with 1 blank line.
Rik <rik@octave.org>
parents:
16994
diff
changeset
|
69 |
31144
22305b923761
doc: which Python executable and how to change it (bug #62733)
Colin Macdonald <cbm@m.fsf.org>
parents:
31137
diff
changeset
|
70 function pyexec = get_python_executable () |
31136
6cfc7af8eacc
python.m: Add code to find python executable on different platforms (bug #62733)
Rik <rik@octave.org>
parents:
30569
diff
changeset
|
71 |
6cfc7af8eacc
python.m: Add code to find python executable on different platforms (bug #62733)
Rik <rik@octave.org>
parents:
30569
diff
changeset
|
72 pyexec = getenv ("PYTHON"); |
6cfc7af8eacc
python.m: Add code to find python executable on different platforms (bug #62733)
Rik <rik@octave.org>
parents:
30569
diff
changeset
|
73 if (isempty (pyexec)) |
6cfc7af8eacc
python.m: Add code to find python executable on different platforms (bug #62733)
Rik <rik@octave.org>
parents:
30569
diff
changeset
|
74 ## PEP394 says Python 3 installs should all provide this command |
6cfc7af8eacc
python.m: Add code to find python executable on different platforms (bug #62733)
Rik <rik@octave.org>
parents:
30569
diff
changeset
|
75 pyexec = "python3"; |
6cfc7af8eacc
python.m: Add code to find python executable on different platforms (bug #62733)
Rik <rik@octave.org>
parents:
30569
diff
changeset
|
76 |
6cfc7af8eacc
python.m: Add code to find python executable on different platforms (bug #62733)
Rik <rik@octave.org>
parents:
30569
diff
changeset
|
77 if (ispc () && (! isunix ())) |
6cfc7af8eacc
python.m: Add code to find python executable on different platforms (bug #62733)
Rik <rik@octave.org>
parents:
30569
diff
changeset
|
78 ## 2020-03: Python.org installer/Anaconda do not provide python3 |
6cfc7af8eacc
python.m: Add code to find python executable on different platforms (bug #62733)
Rik <rik@octave.org>
parents:
30569
diff
changeset
|
79 pyexec = "python"; |
6cfc7af8eacc
python.m: Add code to find python executable on different platforms (bug #62733)
Rik <rik@octave.org>
parents:
30569
diff
changeset
|
80 endif |
6cfc7af8eacc
python.m: Add code to find python executable on different platforms (bug #62733)
Rik <rik@octave.org>
parents:
30569
diff
changeset
|
81 endif |
6cfc7af8eacc
python.m: Add code to find python executable on different platforms (bug #62733)
Rik <rik@octave.org>
parents:
30569
diff
changeset
|
82 |
6cfc7af8eacc
python.m: Add code to find python executable on different platforms (bug #62733)
Rik <rik@octave.org>
parents:
30569
diff
changeset
|
83 endfunction |
6cfc7af8eacc
python.m: Add code to find python executable on different platforms (bug #62733)
Rik <rik@octave.org>
parents:
30569
diff
changeset
|
84 |
19215
c61deba773ab
Add BIST test to perl() and python().
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
85 |
31137
0f4d16af143b
perl.m, python.m: Redo input validation.
Rik <rik@octave.org>
parents:
31136
diff
changeset
|
86 ## Test input validation |
0f4d16af143b
perl.m, python.m: Redo input validation.
Rik <rik@octave.org>
parents:
31136
diff
changeset
|
87 %!error <Invalid call> python () |
0f4d16af143b
perl.m, python.m: Redo input validation.
Rik <rik@octave.org>
parents:
31136
diff
changeset
|
88 %!error <SCRIPTFILE must be a non-empty string> python (123) |
0f4d16af143b
perl.m, python.m: Redo input validation.
Rik <rik@octave.org>
parents:
31136
diff
changeset
|
89 %!error <SCRIPTFILE must be a non-empty string> python ("") |
0f4d16af143b
perl.m, python.m: Redo input validation.
Rik <rik@octave.org>
parents:
31136
diff
changeset
|
90 %!error <ARGUMENTS must be strings> python ("pythonfile", 123) |