如何使用Emacs运行Python代码?

2024-04-19 07:29:19 发布

您现在位置:Python中文网/ 问答频道 /正文

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


Tags: 文件代码py命令缓冲区emacscompile
3条回答

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

在python缓冲区中:

  • C-C C-z:打开python外壳
  • 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中的自动完成功能,它不如以前那么有效。

How do you run Python code using Emacs?

我正在运行Emacs 26,vanilla dev版本(从Savannah克隆的源代码中自行编译)。

(注意,在emacs文档中,我们通常看到,例如,Ctrl-c表示为c-c)

在Python模式下(我通常通过使用C-xc-f“查找”以.py结尾的(可能是新的)文件来进入该模式),可以使用启动Python shell,然后执行缓冲区的
if __name__ == '__main__':使用:

  • C-C C-p(它执行run python以创建一个具有次python主模式、shell Compile次模式的shell)

  • C-u C-C C-C(使用前缀参数执行python shell发送缓冲区)

我们需要prefix参数将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

查看PythonShell发送缓冲区的帮助(通过单击它),我们看到:

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.

根据docsC-u是一个前缀参数,似乎是最通用的参数。

避免使用前缀参数C-u的解决方法是使用括号:

if (__name__ == '__main__'):
    main()

而不是通常的:

if __name__ == '__main__':
    main()

然后C-C C-C自己执行主功能。

在我看来,M-M-&;被低估。通常你只想启动当前正在运行的脚本,不需要把事情复杂化。

当然,只要不需要提供交互输入,就可以使用M-x compile。如果您必须提供交互式输入,M-x shell是您的朋友。

如果您想用一个按键来运行数据,请查看F3F4来录制键盘宏并重放它(宏也可以绑定到键,例如F5)。

在每一种情况下,都没有“魔法”发生。Emacs不知道如何“编译”或“运行”python脚本。必须提供/覆盖合理的命令行调用,例如:

Compile Command: ipython <yourscriptname>.py

python子shell很酷,其中REPL开发风格很有意义,但至少在Windows matplotlibtkinter和其他必须处理Windows消息循环的库中,显示GUI元素时往往会阻塞/挂起。

相关问题 更多 >