0

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

 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 中。

4

2 回答 2

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)))
于 2016-06-07T18:08:38.490 回答
0

;; 这应该可以,但不要在您的 .emacs 文件中:

(defun mp-add-python-keys ()
  (interactive)
  (local-set-key (kbd "C-c C-n") 'py-execute-line))
(add-hook 'python-mode-hook 'mp-add-python-keys)
于 2021-08-27T16:42:02.127 回答