Emacs Org Mode: Executing simple python code

倾然丶 夕夏残阳落幕 提交于 2019-12-03 13:04:24

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.

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))))
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!