你如何使用 Emacs 运行 Python 代码? [英] How do you run Python code using Emacs?

查看:34
本文介绍了你如何使用 Emacs 运行 Python 代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 compile commands . Using M-x compile, I get a compilation buffer that crashes (It says Python is compiling, but then nothing happens).

推荐答案

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

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

在 python 缓冲区中:

In a python buffer:

  • C-c C-z : 打开一个 python shell
  • C-c C-c : 在打开的 python shell 中运行缓冲区的内容
  • C-c C-r : 在 python shell 中运行选定的区域

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

default python shell is "python", if you need to use ipython you can use this conf in your .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'''))
"
 python-shell-completion-string-code
 "';'.join(get_ipython().Completer.all_completions('''%s'''))
")

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

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

ipython>=5 有一个自动完成功能,它打破了 emacs 子外壳,你可以通过改变这一行来解决这个问题

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"

并添加--simple-prompt.

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

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.

这篇关于你如何使用 Emacs 运行 Python 代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆