你如何使用Emacs运行Python代码?

qua*_*ark 25 python emacs

我正在尝试使用Emacs运行Python代码进行测试和调试.我应该如何在*.py文件中调试和运行代码?我尝试使用这些M-x compile命令.使用M-x compile,我得到一个崩溃的编译缓冲区(它说Python正在编译,但没有任何反应).

Obe*_*rix 39

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

在python缓冲区中:

  • Cc Cz:打开一个python shell
  • Cc Cc:在打开的python shell中运行缓冲区的内容
  • Cc Cr:在python shell中运行选定的区域

默认的python shell是"python",如果你需要使用ipython,你可以在你的.emacs中使用这个conf

(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")
Run Code Online (Sandbox Code Playgroud)

只要你的系统安装了ipython当然:)

ipython> = 5有一个自动完成功能,它打破了emacs子shell,您可以通过更改此行 python-shell-interpreter-args "--colors=Linux --profile=default" 并添加来修复此问题 --simple-prompt.

它将允许您正确地看到ipython但由于某种原因我还没有得到emacs中的自动完成效果不如以前那么有效.


Aar*_*all 14

你如何使用Emacs运行Python代码?

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

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

在Python模式下(我通常使用Cx Cf来"查找"以.py结尾的(可能是新的)文件),您可以启动Python shell,然后使用以下命令执行缓冲区
if __name__ == '__main__'::

  • Cc Cp(执行run-python以创建具有Inferior Python主模式的shell,Shell-Compile minor模式)

  • Cu Cc Cc(使用前缀参数执行python-shell-send-buffer)

我们需要使用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
Run Code Online (Sandbox Code Playgroud)

检查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.
Run Code Online (Sandbox Code Playgroud)

根据文档, Cu是一个前缀参数 - 似乎是最通用的.

允许我们避免使用前缀参数Cu的解决方法是使用括号:

if (__name__ == '__main__'):
    main()
Run Code Online (Sandbox Code Playgroud)

而不是通常的:

if __name__ == '__main__':
    main()
Run Code Online (Sandbox Code Playgroud)

然后Cc Cc本身执行主函数.


小智 5

在我看来,M-!M-&被低估了。通常您只想启动当前正在处理的脚本,无需使事情复杂化。

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

如果您想通过一键按下来运行某些内容,请查看F3F4录制键盘宏并重播它(宏也可以绑定到一个键,例如F5)。

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

Compile Command: ipython <yourscriptname>.py
Run Code Online (Sandbox Code Playgroud)

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