Emacs组织模式:执行简单的python代码 [英] Emacs Org Mode: Executing simple python code

查看:108
本文介绍了Emacs组织模式:执行简单的python代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Emacs的组织模式下执行非常简单的Python代码?

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)


推荐答案

两种方式可以使
的结果源代码块-输出。您将它们混合在一起,从而产生了麻烦。

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

第一个块很好。

要修复第二个块:

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

要修复第三个块:

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

当输出模式为 value 时,必须 return 。就像把
1 + 1 放在那一样就不会了。
在第三个中,您希望将结果打印输出,但是默认会话
设置为 value (我的默认值为输出 btw)。

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

关于 org-confirm-babel-evaluate 与该问题无关。
我只是将其设置为 nil

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

这篇关于Emacs组织模式:执行简单的python代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆