I am using Emacs 24/Org 7.8 and am having some issues with org-babel. I want to call a block of code with #+call:
#+name: square(x)
#+begin_src python
return x*x
#+end_src
#+call: square(x=6)
Evaluating #+call with C-c C-c gives the message:
variable "x" in block "square" must be assigned a default value
Any advice on how to debug this? I have the following code related org-babel in my init.el:
(org-babel-do-load-languages
'org-babel-load-languages
'((R . t)
(python . t)
(js . t)
(scheme . t)
(C . t)
(lilypond . t)
(maxima . t)
(octave . t)))
Adding :var x=1
to the begin_src
line like so:
#+name: square(x)
#+begin_src python :var x=1
return x*x
#+end_src
#+call: square(x=6)
don't stop me from receiving the error.
Investigating with the emacs debugger, I find that ref
isn't holding "x=6"
, but is instead holding "x"
.
(org-babel-merge-params
(mapcar
(lambda (ref) (cons :var ref))
(mapcar
(lambda (var) ;; check that each variable is initialized
(if (string-match ".+=.+" var)
var
(error
"variable \"%s\"%s must be assigned a default value"
var (if name (format " in block \"%s\"" name) ""))))
(org-babel-ref-split-args (match-string 5))))
(nth 2 info))
Treating your questions as two parts (issue with variable X and issue with emacs-lisp).
Emacs-LispI would suggest making sure you include (emacs-lisp . t)
in your list of org-babel-load-languages
. That should resolve the issue with it not finding emacs-lisp. You can also try M-: (require 'ob-emacs-lisp)
and your issue should be resolved (it forces it to load the emacs-lisp babel code).
I had to do a few tests to figure out exactly where the issue was, it turns out there's two ways to declare variables when trying to run Org Babel blocks. I'm using emacs-lisp for the test because I don't have a python interpreter installed at the moment, but the results should be equivalent. The error message is telling you that you must by default declare a value for your variable (x=1)
. Once you do that your code blocks should be fine.
* Declare variable in name
#+name: square(x=1)
#+begin_src emacs-lisp
(* x x)
#+end_src
#+results: square
: 1
#+call: square(x=45)
#+results: square(x=45)
: 2025
#+call: square(5)
#+results: square(5)
: 25
* Declare variable in begin_src
#+name: square2
#+begin_src emacs-lisp :var x=1
(* x x)
#+end_src
#+results: square2
: 1
#+call: square2(5)
#+results: square2(5)
: 25
#+call: square2(x=45)
#+results: square2(x=45)
: 2025