Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Emacs Org Mode: Executing simple python code

How can I execute very simple Python-Code in Emacs' Org Mode?

The first example works fine, however I can't make it give me the result of simplest computations:

; works
#+begin_src python
def foo(x):
  if x>0:
    return x+10

  else:
    return x-1

return foo(50)
#+end_src

#+RESULTS:
: 60

; does not work
#+begin_src python
1+1
#+end_src

#+RESULTS:
: None

; does not work
#+begin_src python
print(1+1)
#+end_src

#+RESULTS:
: None

I set up Org Mode using the following lines:

;; enable python for in-buffer evaluation
(org-babel-do-load-languages
 'org-babel-load-languages
 '((python . t)))

;; all python code be safe
(defun my-org-confirm-babel-evaluate (lang body)
(not (string= lang "python")))
(setq org-confirm-babel-evaluate 'my-org-confirm-babel-evaluate)
like image 939
Uwe Ziegenhagen Avatar asked Sep 03 '13 18:09

Uwe Ziegenhagen


People also ask

How do I run code in Org mode?

Org provides many ways to execute code blocks. C-c C-c or C-c C-v e with the point on a code block143 calls the org-babel-execute-src-block function, which executes the code in the block, collects the results, and inserts them in the buffer. By calling a named code block144 from an Org mode buffer or a table.

Does Emacs come with Org mode?

Emacs has included Org Mode as a major mode by default since 2006. Bastien Guerry is the current maintainer, in cooperation with an active development community. Since its success in Emacs, some other systems now provide functions to work with org files.


2 Answers

There are two ways of getting the result of a source block - output and value. You mixed them up, hence the troubles.

First block is fine.

To fix the second block:

#+begin_src python :results value
return 1+1
#+end_src

To fix the third block:

#+begin_src python :results output
print 1+1
#+end_src

When output mode is value you must return. Just putting it there like you did with 1+1 won't do. In the third one you want the result to be printed output, but your default session setting is value(mine defaults to output btw).

And this bit about org-confirm-babel-evaluate is kind of irrelevant to the question. I just have it set to nil.

like image 189
abo-abo Avatar answered Oct 11 '22 15:10

abo-abo


You may still face problems like blank lines cause error in function definition. The solution is given in original thread. I also posted below

(setq org-babel-python-command "ipython3 --no-banner --classic --no-confirm-exit")

;; use %cpaste to paste code into ipython in org mode
(defadvice org-babel-python-evaluate-session
(around org-python-use-cpaste
        (session body &optional result-type result-params) activate)
        "Add a %cpaste and '--' to the body, so that ipython does the right thing."
(setq body (concat "%cpaste\n" body "\n--"))
ad-do-it
(if (stringp ad-return-value)
  (setq ad-return-value (replace-regexp-in-string "\\(^Pasting code; enter '--' alone on the line to stop or use Ctrl-D\.[\r\n]:*\\)" "" ad-return-value))))
like image 26
bowen.li Avatar answered Oct 11 '22 14:10

bowen.li