ob-python: Changed options for default Python command

ob-python will now use the same settings as `run-python' when starting
interactive sessions, by default.

* lisp/ob-python.el (org-babel-python-command): Changed to have
additional `auto' option, which is the new default value.
(org-babel-python-command-session): New option to control default
session Python command.
(org-babel-python-command-nonsession): New option to control default
nonsession Python command.
(org-babel-python--command): New function to determine the command to
run Python.
(org-babel-python-initiate-session-by-key): Call `run-python' without
CMD arg.  Instead, set `python-shell-interpreter' and
`python-shell-interpreter-args' from `org-babel-python--command' when
needed.
(org-babel-python-evaluate-external-process): Use
`org-babel-python--command' to start Python.
This commit is contained in:
Jack Kamm 2023-12-29 13:22:18 -08:00
parent d526ab5388
commit 9239b0e8d1
2 changed files with 81 additions and 12 deletions

View File

@ -551,6 +551,36 @@ Currently implemented options are:
The capture template expansion element =%K= creates links using
~org-store-link~, which respects the values of ~org-id-link-to-use-id~.
*** Changes to ~org-babel-python-command~, and new session/nonsession specific options
The default Python command used by interactive sessions has been
changed to match ~python-shell-interpreter~ and
~python-shell-interpreter-args~ by default. The default Python
command for nonsessions has not changed.
New options ~org-babel-python-command-nonsession~ and
~org-babel-python-command-session~ control the default Python command
for nonsessions and sessions, respectively. By default,
~org-babel-python-command-session~ is ~auto~, which means to use the
configuration for ~python-shell-interpreter(-args)~ as default.
The old option ~org-babel-python-command~ has been changed to have
default value of ~auto~. When not ~auto~, it overrides both
~org-babel-python-command-nonsession~ and
~org-babel-python-command-session~. Therefore, users who had
previously set ~org-babel-python-command~ will not experience any
changes.
Likewise, users who had neither set ~org-babel-python-command~ nor
~python-shell-interpreter(-args)~ will not see any changes -- ~python~
remains the default command.
The main change will be for users who did not configure
~org-babel-python-command~, but did configure
~python-shell-interpreter~, e.g. to use IPython. In this case,
~ob-python~ will now start interactive sessions in a more consistent
manner with ~run-python~.
** New features
*** =ob-plantuml.el=: Support tikz file format output

View File

@ -46,10 +46,28 @@
(python . :any))
"Python-specific header arguments.")
(defcustom org-babel-python-command "python"
"Name of the command for executing Python code."
:version "24.4"
:package-version '(Org . "8.0")
(defcustom org-babel-python-command 'auto
"Command (including arguments) for interactive and non-interactive Python code.
When not `auto', it overrides `org-babel-python-command-session'
and `org-babel-python-command-nonsession'."
:package-version '(Org . "9.7")
:group 'org-babel
:type '(choice string (const auto)))
(defcustom org-babel-python-command-session 'auto
"Command (including arguments) for starting interactive Python sessions.
If `auto' (the default), uses the values from
`python-shell-interpreter' and `python-shell-interpreter-args'.
If `org-babel-python-command' is set, then it overrides this
option."
:package-version '(Org . "9.7")
:group 'org-babel
:type '(choice string (const auto)))
(defcustom org-babel-python-command-nonsession "python"
"Command (including arguments) for executing non-interactive Python code.
If `org-babel-python-command' is set, then it overrides this option."
:package-version '(Org . "9.7")
:group 'org-babel
:type 'string)
@ -246,6 +264,20 @@ be removed after minimum supported version reaches emacs29."
(buffer-substring-no-properties
(car prompt) (cdr prompt)))))
(defun org-babel-python--command (is-session)
"Helper function to return the Python command.
This checks `org-babel-python-command', and then
`org-babel-python-command-session' (if IS-SESSION) or
`org-babel-python-command-nonsession' (if not IS-SESSION). If
IS-SESSION, this might return `nil', which means to use
`python-shell-calculate-command'."
(or (unless (eq org-babel-python-command 'auto)
org-babel-python-command)
(if is-session
(unless (eq org-babel-python-command-session 'auto)
org-babel-python-command-session)
org-babel-python-command-nonsession)))
(defvar-local org-babel-python--initialized nil
"Flag used to mark that python session has been initialized.")
(defun org-babel-python--setup-session ()
@ -267,13 +299,21 @@ initialized session."
(let* ((session (if session (intern session) :default))
(py-buffer (or (org-babel-python-session-buffer session)
(org-babel-python-with-earmuffs session)))
(cmd (if (member system-type '(cygwin windows-nt ms-dos))
(concat org-babel-python-command " -i")
org-babel-python-command))
(python-shell-buffer-name
(org-babel-python-without-earmuffs py-buffer))
(existing-session-p (comint-check-proc py-buffer)))
(run-python cmd)
(existing-session-p (comint-check-proc py-buffer))
(cmd (org-babel-python--command t)))
(if cmd
(let* ((cmd-split (split-string-and-unquote cmd))
(python-shell-interpreter (car cmd-split))
(python-shell-interpreter-args
(combine-and-quote-strings
(append (cdr cmd-split)
(when (member system-type
'(cygwin windows-nt ms-dos))
(list "-i"))))))
(run-python))
(run-python))
(with-current-buffer py-buffer
(if existing-session-p
;; Session was created outside Org. Assume first prompt
@ -374,7 +414,7 @@ the last statement in BODY, as elisp. If GRAPHICS-FILE is
non-nil, then save graphical results to that file instead."
(let ((raw
(pcase result-type
(`output (org-babel-eval org-babel-python-command
(`output (org-babel-eval (org-babel-python--command nil)
(concat preamble (and preamble "\n")
(if graphics-file
(format org-babel-python--output-graphics-wrapper
@ -382,8 +422,7 @@ non-nil, then save graphical results to that file instead."
body))))
(`value (let ((results-file (or graphics-file
(org-babel-temp-file "python-"))))
(org-babel-eval
org-babel-python-command
(org-babel-eval (org-babel-python--command nil)
(concat
preamble (and preamble "\n")
(format