如何使用Emacs运行Python代码?

38

我想在Emacs中运行Python代码以进行测试和调试。 我应该如何调试并运行*.py文件中的代码? 我尝试使用M-x compile命令。 使用M-x compile,我得到一个崩溃的编译缓冲区(它说Python正在编译,但是什么也没有发生)。

4个回答

53

如果您使用的是emacs24,那么这应该是默认设置(在emacs23中,需要使用python.el而不是python-mode.el):

在Python缓冲区中:

  • C-c C-z:打开Python shell
  • C-c C-c:在已打开的Python shell中运行缓冲区的内容
  • C-c C-r:在Python shell中运行所选区域

默认的Python shell是"python",如果您需要使用ipython,可以在您的.emacs配置文件中使用此配置。

(setq
 python-shell-interpreter "ipython"
 python-shell-interpreter-args "--colors=Linux --profile=default"
 python-shell-prompt-regexp "In \\[[0-9]+\\]: "
 python-shell-prompt-output-regexp "Out\\[[0-9]+\\]: "
 python-shell-completion-setup-code
 "from IPython.core.completerlib import module_completion"
 python-shell-completion-module-string-code
 "';'.join(module_completion('''%s'''))\n"
 python-shell-completion-string-code
 "';'.join(get_ipython().Completer.all_completions('''%s'''))\n")

假设您的系统中已经安装了IPython :)

IPython>=5具有自动完成功能,但会破坏Emacs子壳,您可以通过更改此行来修复该问题

python-shell-interpreter-args "--colors=Linux --profile=default"

并添加 --simple-prompt

这将使您能够正确查看IPython,但出于某种原因,我尚未获得emacs中的自动完成效果不如以前。


代码的输出只能显示在minibuffer中,而不是Python shell中吗? - alper

30
我该如何使用Emacs运行Python代码?
我正在运行Emacs 26,原始开发版本(从Savannah克隆源代码后自行编译)。
(注意,在Emacs文档中,我们通常会看到例如Ctrl-c表示为C-c)
在Python模式下(我通常通过使用C-x C-f查找以.py结尾的(可能是新的)文件进入该模式),您可以使用以下方式启动Python shell,并执行您缓冲区的if __name__ == '__main__':
  • C-c C-p(执行run-python以创建带有Inferior Python主模式、Shell-Compile副模式的shell)

  • C-u C-c C-c(带有前缀参数执行python-shell-send-buffer)

我们需要前缀参数将if __name__ == '__main__':块发送到较低级别的Python shell。
我们可以用 Ctrl-c ? 来查看所有的 Ctrl-c 命令。
C-c C-c       python-shell-send-buffer
C-c C-d       python-describe-at-point
C-c C-f       python-eldoc-at-point
C-c C-j       imenu
C-c C-l       python-shell-send-file
C-c C-p       run-python
C-c C-r       python-shell-send-region
C-c C-s       python-shell-send-string
C-c C-t       Prefix Command
C-c C-v       python-check
C-c C-z       python-shell-switch-to-shell
C-c <     python-indent-shift-left
C-c >     python-indent-shift-right

C-c C-t c python-skeleton-class
C-c C-t d python-skeleton-def
C-c C-t f python-skeleton-for
C-c C-t i python-skeleton-if
C-c C-t m python-skeleton-import
C-c C-t t python-skeleton-try
C-c C-t w python-skeleton-while

查看python-shell-send-buffer的帮助(点击它),我们可以看到:

python-shell-send-buffer is an interactive compiled Lisp function in
‘python.el’.

(python-shell-send-buffer &optional SEND-MAIN MSG)

Send the entire buffer 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.
根据文档,C-u是一个前缀参数,似乎是最通用的参数。
避免使用前缀参数C-u的一种解决方法是使用括号:
if (__name__ == '__main__'):
    main()

与通常的不同:

if __name__ == '__main__':
    main()

然后仅使用C-c C-c执行主函数。


感谢您详细的解释。我有点困惑为什么需要 C-c C-cC-u 前缀。文档字符串说:“这将使 Python 习语 if name == 'main' 为 false,以避免意外执行代码”,但是执行代码正是 C-c C-c 的全部意义所在。您是否知道是否有一种 Emacs 方法可以避免使用 C-u 前缀而不是更改 Python 语法为非标准语法?C-c C-cC-u C-c C-c 快得多... - AstroFloyd

5
在我看来,M-!M-&被低估了。通常你只需要启动当前正在工作的脚本,不需要把事情复杂化。
当然,您可以使用M-x compile,只要您不必提供交互式输入。如果您需要提供交互式输入,M-x shell是您的好朋友。
如果您想用一个按键运行一些东西,请查看F3F4以记录并重播键盘宏(该宏也可以绑定到一个键上,例如F5)。
在每种情况下,没有“魔法”发生。Emacs不知道如何“编译”或“运行”Python脚本。您必须提供/覆盖合理的命令行调用,例如:
Compile Command: ipython <yourscriptname>.py

Python的子shell很棒,适合使用REPL开发风格,但至少在Windows上,matplotlibtkinter和其他需要处理Windows消息循环的库在显示GUI元素时往往会阻塞/挂起。


4

在Emacs中打开Python文件后,您需要使用以下命令启动Python进程:

M-x run-pythonC-c C-p,这将创建一个下属的Python shell缓冲区。该缓冲区将通过水平分割创建,并且活动缓冲区将包含Python文件。

接下来,您可以执行C-c C-c,将当前Python缓冲区发送到下面的下属Python shell中。这是您将看到Python文件的输出的地方。

要在Python文件缓冲区和下属Python shell缓冲区之间切换,您可以执行C-x o

如果您意外关闭了其中一个缓冲区,您可以使用C-x C-<left_arrow>C-x C-<right_arrow>在缓冲区之间切换,并执行像Aaron Hall提到的Python缓冲区操作。

注意:

  1. 运行不含任何扩展的GNU Emacs 26.2。

  2. 未定义if (__name__ == '__main__'):块。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接