在我的终端 shell 中,如果我只是评估,例如3+3,我得到了
Python 3.6.1 (default, May 21 2017, 04:38:38)
[GCC 4.2.1 Compatible Apple LLVM 8.1.0 (clang-802.0.42)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 3+3
6
>>>
但是,使用 emacs 的交互式 shell 以及使用 python-shell-send-line 或 python-shell-send-region 评估代码时,我不明白对正在执行的代码的任何反馈(当然除非涉及到一些 print)。
我在.emacs中的相关配置是这样的:
(setq python-shell-interpreter "/opt/local/bin/python3.6"
python-shell-interpreter-args "-i")
(add-hook 'python-mode-hook 'anaconda-mode)
我不确定它是否相关,但即使我确实安装了 readline(如您在图片中所见),我也会收到此警告:
Warning (python): Your ‘python-shell-interpreter’ doesn’t seem to support readline, yet ‘python-shell-completion-native’ was t and "python3.6" is not part of the ‘python-shell-completion-native-disabled-interpreters’ list. Native completions have been disabled locally.
我希望看到有关我执行的代码的一些反馈。有什么想法吗?
请您参考如下方法:
我使用以下绑定(bind)到 C-c C-c。如果该行不是以 YMMV 开头,则发送打印语句只是一个简单的 hack。
;; send current line, with prefix print result
(defun python--send-line (arg)
(interactive "P")
(if (not arg)
(python-shell-send-region (line-beginning-position) (line-end-position))
(save-excursion
(beginning-of-line)
(if (looking-at-p "\\s-*print")
(python-shell-send-region (line-beginning-position)
(line-end-position))
(python-shell-send-string
(concat "print(" (buffer-substring (line-beginning-position)
(line-end-position))
")")))))
(display-buffer (process-buffer (python-shell-get-process))))
