我正在尝试使用Emacs运行Python代码进行测试和调试。我应该如何在* .py文件中调试和运行代码?我尝试使用M-x compile
命令。使用M-x compile
,我得到一个崩溃的编译缓冲区(它说Python正在编译,但没有任何反应)。
答案 0 :(得分:34)
如果你使用的是emacs24,那么这应该是默认的(在emacs23中你需要python.el,而不是python-mode.el):
在python缓冲区中:
默认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")
前提是你的系统中安装了ipython:)
ipython> = 5有一个自动完成功能,打破了emacs子shell,您可以通过更改此行来解决此问题
python-shell-interpreter-args "--colors=Linux --profile=default"
并添加--simple-prompt
。
它可以让你正确地看到ipython但是由于某些原因我还没有得到emacs中的自动完成功能不像以前那么有效。
答案 1 :(得分:9)
如何使用Emacs运行Python代码?
我正在运行Emacs 26,vanilla dev版本(从Savannah克隆的源代码自编译。)
(请注意,在emacs文档中,我们通常会看到,例如, Ctrl - c 表示为C-c)
在Python模式下(我通常使用C-x C-f输入“查找”以.py结尾的(可能是新的)文件),你可以启动一个Python shell,然后执行你的缓冲区的
if __name__ == '__main__':
with:
C-c C-p(执行run-python以创建具有Inferior Python主模式的shell,Shell-Compile minor模式)
C-u C-c C-c(使用前缀参数执行python-shell-send-buffer)
我们需要使用prefix参数将if __name__ == '__main__':
块发送到劣质Python shell。
我们可以通过 Ctrl - c 看到所有 Ctrl - c 命令吗? / KBD>
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.
根据docs C-u是前缀参数 - 似乎是最通用的。
允许我们避免使用前缀参数C-u的解决方法是使用括号:
if (__name__ == '__main__'):
main()
而不是通常的:
if __name__ == '__main__':
main()
然后C-c C-c本身执行主函数。
答案 2 :(得分:1)
在我看来, M - !和 M - &amp; 被低估了。通常你只想启动你正在处理的当前脚本,不需要复杂化。
当然,您可以使用M-x compile
,只要您不必提供交互式输入即可。如果你做必须提供互动输入,M-x shell
是你的朋友。
如果你想按一个按钮来运行东西,请查看 F3 和 F4 来录制键盘宏并重放它(宏也可以绑定到密钥,例如 F5 )。
在每一种情况下,都没有“神奇”发生。 Emacs不知道如何“编译”或“运行”python脚本。您必须提供/覆盖合理的命令行调用,例如:
Compile Command: ipython <yourscriptname>.py
python子shell很酷,REPL开发风格很有意义,但至少在Windows matplotlib
上,tkinter
和其他必须处理Windows消息循环的库往往会阻塞/挂起显示GUI元素。
答案 3 :(得分:1)
在Emacs中打开python文件后,您将需要使用以下命令启动python进程:
M-x run-python
或C-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缓冲区执行操作。
注意:
运行GNU Emacs 26.2,没有任何扩展。
未定义if (__name__ == '__main__'):
块