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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25669809/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 23:28:23  来源:igfitidea点击:

How do you run Python code using Emacs?

pythonemacs

提问by quark

I'm trying to run Python code for testing and debugging using Emacs. How should I debug and run code in *.py files ? I tried using the M-x compilecommands . Using M-x compile, I get a compilation buffer that crashes (It says Python is compiling, but then nothing happens).

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

回答by Oberix

If you are using emacs24 this should be the default (in emacs23 you need python.el, not python-mode.el):

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

In a python buffer:

在 python 缓冲区中:

  • C-c C-z : open a python shell
  • C-c C-c : run the content of the buffer in the opened python shell
  • C-c C-r : run the selected region in the python shell
  • Cc Cz:打开一个python shell
  • Cc Cc : 在打开的python shell中运行缓冲区的内容
  • Cc Cr : 在 python shell 中运行选定的区域

default python shell is "python", if you nee to use ipython you can use this conf in your .emacs

默认 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")

provided that you have ipython installed in your system of course :)

当然,前提是您的系统中安装了 ipython :)

ipython>=5 has a auto-complete feature which breaks the emacs sub-shell, you can fix this by changing this line python-shell-interpreter-args "--colors=Linux --profile=default" and add --simple-prompt.

ipython>=5 具有自动完成功能,该功能会破坏 emacs 子外壳,您可以通过更改此行 python-shell-interpreter-args "--colors=Linux --profile=default" 并添加--simple-prompt.

It will allow you to see ipython correctly but for some reason I did not get yet the auto-completion in emacs is not as effective as it used to be.

它可以让你正确地看到 ipython,但由于某种原因,我还没有得到 emacs 中的自动完成功能不像以前那么有效。

回答by Aaron Hall

How do you run Python code using Emacs?

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

I'm running Emacs 26, vanilla dev version (self compiled from source cloned from Savannah).

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

(Note that in emacs docs, we usually see, for example, Ctrl-cdenoted as C-c)

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

In Python mode (which I usually enter by using C-x C-f to "find" a (possibly new) file ending in .py), you can start a Python shell with and then execute your buffer's
if __name__ == '__main__':with:

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

  • C-c C-p (which executes run-python to create a shell with Inferior Python major mode, Shell-Compile minor mode)

  • C-u C-c C-c (which executes python-shell-send-buffer with a prefix argument)

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

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

We require the prefix argument to send the if __name__ == '__main__':block to the inferior Python shell.

我们需要前缀参数将if __name__ == '__main__':块发送到低级 Python shell。

We can see all of the Ctrl-ccommands with Ctrl-c?

我们可以看到所有的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
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

Inspecting the help for python-shell-send-buffer (by clicking it), we see:

检查 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.
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.

According to the docsC-u is a prefix argument - and seems to be the most generic one.

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

A workaround that lets us avoid using the prefix argument C-u is using parentheses:

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

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

instead of the usual:

而不是通常的:

if __name__ == '__main__':
    main()

and then C-c C-c by itself executes the main function.

然后 Cc Cc 自己执行 main 函数。

回答by T Nierath

In my opinion, M-!and M-&are underrated. Often you just want to start the current script you are working on, no need to complicate things.

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

Of course, you can use M-x compile, as long as you don't have to provide interactive input. If you dohave to provide interactive input, M-x shellis your friend.

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

If you want to run stuff with one button press, check out F3and F4to record a keyboard macro and replay it (the macro can also be bound to a key, e.g. F5).

如果你想一键运行东西,检查F3F4记录键盘宏并重播它(宏也可以绑定到一个键,例如F5)。

In each of these cases, there is no "magic" taking place. Emacs does not know how to "compile" or "run" python scripts. You have to provide/overwrite a sensible command line invocation, like:

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

Compile Command: ipython <yourscriptname>.py

The python subshell is cool where a REPL development style makes sense, but at least on Windows matplotlib, tkinterand other libraries that have to deal with the Windows Message Loop tend to block/hang upon displaying GUI elements.

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

回答by T Nierath

Once you open your python file in Emacs, you will need to start the python process with: M-x run-pythonor C-c C-p, which creates an inferior python shell buffer. This buffer will be created by a horizontal split, and the active buffer will be the one containing the python file.

一旦你在 Emacs 中打开你的 python 文件,你将需要使用: M-x run-pythonor启动 python 进程 C-c C-p,这会创建一个劣质的 python shell 缓冲区。该缓冲区将由水平拆分创建,活动缓冲区将是包含 python 文件的缓冲区。

Then you can do C-c C-c, which will send the current python buffer to the inferior python shell below. This is where you will see the output of your python file.

然后你可以做C-c C-c,这会将当前的python缓冲区发送到下面的劣等python shell。您将在此处看到 Python 文件的输出。

To switch to and from your python file buffer and the inferior python shell buffer, you can do C-x o.

要在 python 文件缓冲区和劣质 python shell 缓冲区之间切换,你可以执行C-x o.

If you accidentally close one of the buffers, you can switch between buffers with C-x C-<left_arrow>and C-x C-<right_arrow>and perform operations on the python buffer like the ones mentioned by Aaron Hall.

如果您不小心关闭了其中一个缓冲区,您可以使用C-x C-<left_arrow>和在缓冲区之间切换,并C-x C-<right_arrow>Aaron Hall提到的那样对 python 缓冲区执行操作。

NOTE:

注意

  1. Running GNU Emacs 26.2 without any extensions.

  2. Did not define if (__name__ == '__main__'):block

  1. 运行没有任何扩展的 GNU Emacs 26.2。

  2. 没有定义if (__name__ == '__main__'):