如何在 emacs 中打开 IPython 解释器?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17817019/
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-19 09:15:23  来源:igfitidea点击:

How to open IPython interpreter in emacs?

pythonemacsipython

提问by Eric Wilson

In order to use IPython during Python development in Emacs, I have been opening up a terminal, and typing ipython. This doesn't seem like the right way to do it.

为了在 Emacs 的 Python 开发过程中使用 IPython,我一直打开一个终端,输入ipython. 这似乎不是正确的方法。

For one thing, my buffer lists this buffer as *ansi-term*, which is confusing, as I often end up with multiple terminals, one of which is dedicated to Python. I also see that I can open a python interpreter directly with M-x python-shell. It seems like there ought to be a similar option for IPython.

一方面,我的缓冲区将此缓冲区列为*ansi-term*,这令人困惑,因为我经常以多个终端结束,其中一个专用于 Python。我还看到我可以直接用M-x python-shell. 似乎 IPython 应该有类似的选项。

Is there a better way to use the IPython interpreter in Emacs?

有没有更好的方法在 Emacs 中使用 IPython 解释器?

I'm using Emacs 24 with Prelude.

我将 Emacs 24 与 Prelude 一起使用。

回答by abo-abo

UPD Detailed instructions for beginners:

UPD 初学者详细说明:

  1. Copy this:

    (defun ipython ()
        (interactive)
        (term "/usr/bin/ipython"))
    
  2. Try first launching Emacs as emacs -qfrom the terminal. -qmeans don't load any customized code. It means that in this mode your emacs will behave exactly like mine.

  3. You should now be at the welcome screen. Press q.
  4. You should now be in the *scratch*buffer. Press C-yto paste the code you copied from the browser.
  5. Position your cursor at the very end of the code, right after the last parenthesis.
  6. Press C-jto evaluate the code. The name ipythonpops up.
  7. Now you can execute this command as any other e.g. M-x ipython.
  8. If you want this change to become permanent, append the code to your ~/.emacs.
  1. 复制这个:

    (defun ipython ()
        (interactive)
        (term "/usr/bin/ipython"))
    
  2. 首先尝试emacs -q从终端启动 Emacs 。 -q意味着不加载任何自定义代码。这意味着在这种模式下,您的 emacs 的行为将与我的完全一样。

  3. 您现在应该在欢迎屏幕上。按q
  4. 您现在应该在*scratch*缓冲区中。按C-y粘贴您从浏览器复制的代码。
  5. 将光标放在代码的最后,紧跟在最后一个括号之后。
  6. 按下C-j以评估代码。名字ipython弹出。
  7. 现在您可以像执行任何其他命令一样执行此命令,例如M-x ipython.
  8. 如果您希望此更改成为永久性更改,请将代码附加到您的~/.emacs.

回答by Randy Morris

Edit: Whoops, the project below is a fork, not the official Prelude project. The second paragraph here still stands. Or you could try the fork.

编辑:哎呀,下面的项目是一个分支,而不是官方的 Prelude 项目。这里的第二段仍然有效。或者你可以试试叉子。

It looks like Prelude already sets up ipython integration. Maybe your copy is out of date?

看起来 Prelude已经设置了 ipython 集成。也许你的副本已经过时了?

If M-x run-pythondoesn't launch ipython, try adding (setq python-shell-interpreter "ipython")to your init file (or just evaluating it) and running M-x run-pythonagain. After a quick test without Prelude that seems to be all that is necessary.

如果M-x run-python没有启动 ipython,请尝试添加(setq python-shell-interpreter "ipython")到您的 init 文件(或只是评估它)并M-x run-python再次运行。在没有 Prelude 的情况下进行了快速测试后,这似乎是所有必要的。

回答by kindahero

I don't know what Prelude is, but if you can install the latest Emacs (i.e, Emacs with Fabian's python-mode included), then you can use the following to let Python mode know your preferred choice of interpreter. Then you just need to call M-x run-python:

我不知道 Prelude 是什么,但是如果您可以安装最新的 Emacs(即包含 Fabian 的 python-mode 的 Emacs),那么您可以使用以下命令让 Python 模式知道您首选的解释器。然后你只需要调用M-x run-python

--

——

(when (executable-find "ipython")
  (setq python-shell-interpreter "ipython"))

--

——

回答by mortonjt

I've tried abo-abo's approach to running ipython inside of emacs. Its a pretty slick way of shortcutting shell functions within emacs. However, you do lose emacs functionality when you are in the shell (e.g. can't M-x oout of the shell)

我试过 abo-abo 在 emacs 中运行 ipython 的方法。它是在 emacs 中快捷地使用 shell 函数的一种非常巧妙的方法。然而,当你在 shell 中时,你确实失去了 emacs 的功能(例如不能M-x o离开 shell)

Instead, I personally think that using the Emacs builtin shell command (M-x shell) is much better. You can run ipython within the Emacs builtin shell and still have all Emacs functionality that we all know and love.

相反,我个人认为使用 Emacs 内置 shell 命令 ( M-x shell) 要好得多。您可以在 Emacs 内置 shell 中运行 ipython,并且仍然拥有我们都知道和喜爱的所有 Emacs 功能。

回答by NetSmoothMF

You can use my Emacs extension called emacs-ipython for this.

为此,您可以使用我的名为 emacs-ipython 的 Emacs 扩展。

https://github.com/burakbayramli/emacs-ipython

https://github.com/burakbayramli/emacs-ipython

In a LaTeX buffer (you can change this to be any buffer) between code blocks with one keystroke it sends the python code to ipython kernel and displays the results in a verbatim block underneath.

在代码块之间的 LaTeX 缓冲区(您可以将其更改为任何缓冲区)中,只需一次击键,它将 python 代码发送到 ipython 内核,并在下面的逐字块中显示结果。

回答by Arun Kumar Khattri

Seems a bit late, however was also having similar problem, wanted to have ipython as default python interpreter for my emacs 24.x. The following worked like a magic -

似乎有点晚了,但是也有类似的问题,希望将 ipython 作为我的 emacs 24.x 的默认 python 解释器。以下工作就像魔术一样 -

(require 'python)
(setq python-shell-interpreter "ipython")
(setq python-shell-interpreter-args "--pylab")

thanks to http://www.flannaghan.com/2013/08/29/ipython-emacsfor saving my day and hopefully yours.

感谢http://www.flaannaghan.com/2013/08/29/ipython-emacs拯救了我的一天,希望你也能如此。

PS: seems having anaconda preinstalled in your system helps...

PS:似乎在您的系统中预装了 anaconda 有帮助...