Org-mode upstream changes allow to switch between IPython and Python

This commit is contained in:
Gerard Vermeulen 2022-08-28 16:39:43 +02:00
parent cd4a380580
commit 82378ef819

View File

@ -718,7 +718,9 @@ defined in this Org file.
(smart-latex-engrave-org-source-blocks :no-manual t)
(toggle-engrave-faces-latex-face-apply-override :no-manual t)
(toggle-engrave-faces-latex-face-mapper-override :no-manual t)
(toggle-org-babel-python-format-session-value-override :no-manual t))))
(toggle-org-babel-python-format-session-value-override :no-manual t)
"Python"
(choose-common-python-interpreter :no-manual t))))
#+end_src
** [[info:info#Top][Info (info)]]
@ -1714,9 +1716,11 @@ buffers with help of an external [[https://nodejs.org][node.js]] program.
:END:
Listing [[lst:customize-org-babel]], [[lst:customize-org]], [[lst:customize-org-link]] does
basic customization of [[https://orgmode.org/][Org-mode]] variables. Listing
[[lst:customize-org-for-lualatex-export]], and
[[lst:customize-ox-latex-for-lualatex-export]] configure [[https://orgmode.org/][Org-mode]] for export to
basic customization of [[https://orgmode.org/][Org-mode]] variables. In addition, listing
[[lst:customize-org-babel]] selects the Python interpreter by means of
src_emacs-lisp{(choose-common-python-interpreter)} defined in listing
[[lst:choose-common-python-interpreter]]. Listing [[lst:customize-org-for-lualatex-export]],
and [[lst:customize-ox-latex-for-lualatex-export]] configure [[https://orgmode.org/][Org-mode]] for export to
LuaLaTeX. Listing [[lst:customize-ob-python]] allows to pretty-print Python session
source block values with [[https://github.com/psf/black#readme][black]] instead of [[https://docs.python.org/3/library/pprint.html][pprint]]. Listing
[[lst:customize-org-link]] configures =org-link= to use relative file path links.
@ -1764,14 +1768,7 @@ backward compatibility. See table [[tab:org-latex-class-tag-placeholder]] and ty
'(org-babel-lisp-eval-fn #'sly-eval))))
(with-eval-after-load 'ob-python
(custom-set-variables
;; Python hangs, but IPython does not.
;; `(org-babel-python-command
;; ,(concat (or (executable-find "python3") (executable-find "python"))
;; " -E"))
`(org-babel-python-command
,(concat (or (executable-find "ipython3") (executable-find "ipython"))
" --simple-prompt --HistoryAccessor.enabled=False"))))
(choose-common-python-interpreter 'python))
#+end_src
#+caption[Customize =ob-python=]:
@ -3565,16 +3562,18 @@ server, before plunging into the configuration steps:
:END:
Listing [[lst:configure-python]] configures [[https://git.savannah.gnu.org/cgit/emacs.git/tree/lisp/progmodes/python.el][python-mode]] to use [[https://flake8.pycqa.org/en/latest/][flake8]] as style
checker and [[https://ipython.org/][IPython]] as shell interpreter. The [[https://github.com/pythonic-emacs/pythonic#readme][pythonic]] and [[https://github.com/jorgenschaefer/pyvenv#readme][pyvenv]] packages
provide support to handle Python virtual environments within Emacs. The [[https://github.com/pyenv/pyenv][pyenv]]
package provides support to work with [[https://github.com/pyenv/pyenv#readme][pyenv]] (eventually with [[https://github.com/pyenv/pyenv-virtualenv#readme][pyenv-virtualenv]])
to select between different python versions (eventually each with different
environments). In the end, all those packages do is to set
=python-shell-virtualenv-root= (in case of [[https://github.com/pyenv/pyenv#readme][pyenv]] and [[https://github.com/pythonic-emacs/pythonic#readme][pythonic]]) and tweak the
environment variables and restart the relevant Python child processes (in case
of [[https://github.com/jorgenschaefer/pyvenv#readme][pyvenv]]). Therefore, this setup replaces those packages with listing
[[lst:manage-pyenv]] to manage [[https://github.com/pyenv/pyenv#readme][pyenv]] from within Emacs and listing
[[lst:setting-python-shell-virtualenv-root]] to set =python-shell-virtualenv-root=.
checker and [[https://ipython.org/][IPython]] as shell interpreter and selects the Python interpreter by
means of src_emacs-lisp{(choose-common-python-interpreter)} defined in listing
[[lst:choose-common-python-interpreter]]. The [[https://github.com/pythonic-emacs/pythonic#readme][pythonic]] and [[https://github.com/jorgenschaefer/pyvenv#readme][pyvenv]] packages provide support
to handle Python virtual environments within Emacs. The [[https://github.com/pyenv/pyenv][pyenv]] package provides
support to work with [[https://github.com/pyenv/pyenv#readme][pyenv]] (eventually with [[https://github.com/pyenv/pyenv-virtualenv#readme][pyenv-virtualenv]]) to select between
different python versions (eventually each with different environments). In the
end, all those packages do is to set =python-shell-virtualenv-root= (in case of
[[https://github.com/pyenv/pyenv#readme][pyenv]] and [[https://github.com/pythonic-emacs/pythonic#readme][pythonic]]) and tweak the environment variables and restart the relevant
Python child processes (in case of [[https://github.com/jorgenschaefer/pyvenv#readme][pyvenv]]). Therefore, this setup replaces
those packages with listing [[lst:manage-pyenv]] to manage [[https://github.com/pyenv/pyenv#readme][pyenv]] from within Emacs
and listing [[lst:setting-python-shell-virtualenv-root]] to set
=python-shell-virtualenv-root=.
#+caption[Configure =python=]:
#+caption: Configure =python=.
@ -3585,15 +3584,50 @@ of [[https://github.com/jorgenschaefer/pyvenv#readme][pyvenv]]). Therefore, thi
`(python-check-command ,(executable-find "flake8"))
`(python-flymake-command '(,(executable-find "flake8") "-"))
'(python-indent-guess-indent-offset nil)
'(python-shell-completion-native-disabled-interpreters '("ipython3" "pypy"))
;; Python hangs, but IPython does not.
;; `(python-shell-interpreter
;; ,(or (executable-find "python3") (executable-find "python")))
;; '(python-shell-interpreter-args "-E -i")
'(python-shell-completion-native-disabled-interpreters '("ipython3" "pypy")))
(choose-common-python-interpreter 'python))
#+end_src
#+caption[Choose common =python= interpreter for =ob-python= and =python-mode=]:
#+caption: Choose =python= interpreter for =ob-python= and =python-mode=.
#+name: lst:choose-common-python-interpreter
#+begin_src emacs-lisp
(defun choose-common-python-interpreter (&optional program)
"Choose Python interpreter PROGRAM for `ob-python' and `python-mode'."
(interactive)
(let* ((prompt (format "Choose Python (%s): "
(bound-and-true-p python-shell-interpreter)))
(choices '(ipython python))
(choice (if (member program choices)
(symbol-name program)
(completing-read prompt choices nil t))))
(when (boundp 'org-babel-python-command)
(pcase choice
("python"
(custom-set-variables
`(org-babel-python-command
,(concat (or (executable-find "python3")
(executable-find "python"))
" -E"))))
("ipython"
(custom-set-variables
`(org-babel-python-command
,(concat (or (executable-find "ipython3")
(executable-find "ipython"))
" --simple-prompt --HistoryAccessor.enabled=False"))))))
(when (boundp 'python-shell-interpreter)
(pcase choice
("python"
(custom-set-variables
`(python-shell-interpreter
,(or (executable-find "python3") (executable-find "python")))
'(python-shell-interpreter-args "-E -i")))
("ipython"
(custom-set-variables
`(python-shell-interpreter
,(or (executable-find "ipython3") (executable-find "ipython")))
'(python-shell-interpreter-args
"--simple-prompt --HistoryAccessor.enabled=False")))
"--simple-prompt --HistoryAccessor.enabled=False")))))))
#+end_src
#+caption[Manage =pyenv=]: