Emacs / Python - 函数期望缩进块

时间:2016-06-07 16:30:42

标签: python function shell emacs elpy

我切换到了Emacs。我在Emacs中使用Elpy作为IDE。我的设置是并排窗口,左边是缓冲区(脚本),我在其中编写/编辑代码,然后使用Ctrl-Enter将代码发送到右侧的IPython shell。当我输入函数时:

 import numpy as np
 import pandas as pd

 data = pd.read_csv('spx_index.csv')

 def convert(x):
     return x.ix[:, 1:].set_index(x.ix[:, 0])

进入脚本(4空格缩进)并按两次Ctrl-Enter我得到:

>>> ...   File "<stdin>", line 2
    return x.ix[:, 1:].set_index(x.ix[:, 0]) 
         ^
IndentationError: expected an indented block

但是,当我复制该函数并将其直接粘贴到IPython shell中时:

>>> def convert(x):
    return x.ix[:, 1:].set_index(x.ix[:, 0]) 
... ... 
>>> 

它可以正常工作并保存功能。

让函数直接从脚本运行到shell将是理想的选择。我无法想象必须将每个函数复制并粘贴到shell中。

1 个答案:

答案 0 :(得分:0)

要发送整个缓冲区,您可以按 C-c C-c 。您可以使用 C-c C-r 发送整个区域,然后通过绑定以下函数发送当前行。以下函数基本上是python-shell-send-buffer

的副本
(defun python-shell-send-line (&optional send-main msg)
  "Send the entire line to inferior Python process.
When optional argument SEND-MAIN is non-nil, allow execution of
code inside blocks delimited by \"if __name__== \\='__main__\\=':\".
When called interactively SEND-MAIN defaults to nil, unless it's
called with prefix argument.  When optional argument MSG is
non-nil, forces display of a user-friendly message if there's no
process running; defaults to t when called interactively."
  (interactive (list current-prefix-arg t))
  (save-restriction
    (widen)
    (python-shell-send-region (line-beginning-position) (line-end-position) send-main msg)))