Is it possible to have, in the same org file, two code blocks in the same language that are run in different interpreters by specifying different options at the top of the code block?
-
2Do you mean, for example, different versions of installed software, for example python26, python27, and python3? Or just unique python sessions, but all using the same executable? @dgtized explains the latter. – mankoff Oct 21 '14 at 13:07
-
2I mean different versions of installed software, i.e., using different executables. – cefstat Oct 21 '14 at 15:13
3 Answers
The original question has been modified to concern running multiple versions of an executable, and not simply independent interpreters.
Using find-library
I inspected the source of ob-ruby
, which includes this code:
(defvar org-babel-ruby-command "ruby"
"Name of command to use for executing ruby code.")
I have seen references elsewhere for python using org-babel-python-command
, so it exists in some other languages, check the appropriate ob-$lang
support to see.
This allows the following to work:
#+begin_src emacs-lisp :results none
(setq org-babel-python-command "python3")
#+end_src
#+begin_src python :results output
import sys
print(sys.version)
#+end_src
#+RESULTS:
: 3.4.0 (default, Apr 11 2014, 13:05:11)
: [GCC 4.8.2]
#+begin_src emacs-lisp :results none
(setq org-babel-python-command "python2")
#+end_src
#+begin_src python :results output
import sys
print(sys.version)
#+end_src
#+RESULTS:
: 2.7.6 (default, Mar 22 2014, 22:59:56)
: [GCC 4.8.2]
This could be combined with :session python3
and :session python2
to avoid calling elisp before each block. It does seem like there should be a simpler way to do this though.

- 4,169
- 20
- 41
-
2There is a `org-babel-post-tangle-hook`. Someone should implement at `org-babel-pre-tangle-hook`. – mankoff Oct 22 '14 at 13:56
-
1I'm not very familiar with the internals, but I'm not sure that tangling would be the appropriate phase to make this change? Honestly, it seems like the block needs an `:interpreter` property. – dgtized Oct 22 '14 at 15:21
-
2I'm not too familiar with it either. Yes `:interpreter` makes sense. But `org-babel-post-tangle-hook` runs after the code execution via `C-c C-c` in a code block. I assume `pre` would run before code execution. But I realize now if changing a global variable, it would have bad side-effects. `:interpreter` would be better. – mankoff Oct 22 '14 at 16:03
-
1Thank you @dgtized and @mankoff for your answers. They pointed me to the right direction. It was my mistake that I had not specified that I was interested in javascript code. Based on your answers I decided to add an `:interpreter` option to `org-babel-execute:js`. But then going through the source for `org-babel-execute:js` I found that there is already a `:cmd` option that does exactly what I want. Unfortunately, `:cmd` is not available for all languages and I also did not find any documentation for `ob-js` so I initially missed `:cmd`'s existence. – cefstat Oct 23 '14 at 09:08
-
@cefstat I noted `:cmd`, but it looked like it was only used to append arguments to the interpreter command. Could you please answer your own question with a full example showing the use of `:cmd` to solve the issue for those that have this issue in the future? – dgtized Oct 24 '14 at 20:18
I believe by default each block runs in an independent interpreter even if it's the same language. The behavior may be different for some languages. For instance, I'm not sure that emacs-lisp blocks support the session property.
#+BEGIN_SRC ruby
a = "foo"
#+END_SRC
#+RESULTS:
: foo
#+BEGIN_SRC ruby
a ||= "bar"
#+END_SRC
#+RESULTS:
: bar
#+BEGIN_SRC ruby :session foo
a ||= "session foo"
#+END_SRC
#+RESULTS:
: session foo
#+BEGIN_SRC ruby :session foo
a += " with bar"
#+END_SRC
#+RESULTS:
: session foo with bar
The first two blocks use independent interpreters, but the third and fourth block share a session :foo
, so that they evaluate in the same interpreter.

- 4,169
- 20
- 41
It turns out that more almost all languages supported by Org Babel there is no option to use a different interpreter for a specific code-block. One notable exception (and the one that interests me) is Javascript. In this case one can use the :cmd
option.
The standard JS interpreter is node
, as defined in the variable org-babel-js-cmd
. To run a specific code block through a different interpreter pass the :cmd
option as in the following example.
#+begin_src js :cmd "/usr/bin/osascript -l JavaScript"
app = Application.currentApplication()
app.includeStandardAdditions = true
app.say("Hello")
#+end_src

- 251
- 2
- 5