Question

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))
Was it helpful?

Solution

Treating your questions as two parts (issue with variable X and issue with emacs-lisp).

Emacs-Lisp

I 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).

Variable Issue

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

OTHER TIPS

EDIT The code below works for versions of org-mode before 7.8. To get this working with 7.8 and beyond, you should use #+name instead of #+srcname.

The two things you list aren't necessarily related.

For the python bit, you should use srcname instead of name. You also need to let the block know that there is an input variable, by specifying the :var header argument. You should also define a default variable here, otherwise python would complain. This is actually misleading in the current online documentation. You want something like:

#+srcname: square
#+begin_src python :var x=1
  return x*x
#+end_src

Upon evaluating #+call: square(x=6) with C-c C-c gives:

#+results: square(x=6)
: 36

The second issue seems to be related to your org-babel-load-languages variable. By default, the emacs-lisp language should be loaded. Are you perhaps changing this in your .emacs file? Your above lisp code works fine for me, and I have the following in my .emacs:

; org-babel stuff
(org-babel-do-load-languages
 'org-babel-load-languages
 '((org . t)
   (emacs-lisp . t)
   (gnuplot . t)
   (python . t)
   (octave . t)
   (latex . t)
   (perl . t)
   (sh . t)
   (ditaa . t)))
(setq org-confirm-babel-evaluate nil)
(require 'org-exp-blocks)

but the (emacs-lisp . t) line shouldn't be necessary.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top