0

I have an org-mode file, with org-mode version 9+ (so my understanding is that Babel should be integrated). Then I have the following in the file

#+BEGIN_SRC python :exports both :results output
return 7 + 7
#+END_SRC

#+RESULTS:

I then compile to a PDF file. My understanding is that this should display both the codeblock and the evaluation of the codeblock. However the PDF just displays:

return 7 + 7

I have tried using print instead of return, but same outcome. What am I misunderstanding here?

  • this doesn't look like valid python. return is only used in a function – manandearth Dec 12 '18 at 22:17
  • Good point, but as I said, I'd tried with print as well, which would have worked with :results output, as that reads from the stdout stream – Ed Prince Dec 12 '18 at 22:19
  • What about 7 + 7 on its own? – manandearth Dec 12 '18 at 22:23
  • 1
  • I discovered I needed to just create an init.el file and load the language in there. Posted the answer below – Ed Prince Dec 12 '18 at 22:25
2

Only if you use the header argument :results value the code of the block is wrapped by a function and the result of the code block is the return value of that function.

#+BEGIN_SRC python :exports both :results value
return 7 + 7
#+END_SRC

On the other hand :results output means that the code of the block is sent to the interpreter. The result of the code block is what the code prints to the standard output.

#+BEGIN_SRC python :exports both :results output
print "%d" % (7 + 7)
#+END_SRC
  • Yeah, I also hadn't figured that out, thanks! – Ed Prince Dec 12 '18 at 22:13
0

So I had misunderstood how the codeblocks were executed. You need an init.el file in ~/emacs.d/ and inside that I needed:

(org-babel-do-load-languages
 'org-babel-load-languages '((python . t)))

Once this was in place, when I convert the file to a PDF, it gives the option to execute the code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.