Emacs 中的 IPython。快速代码评估
更新:该问题仍缺乏令人满意的答案。
我想将代码片段“发送”到 Emacs 23.2 (Linux) 中的 IPython 解释器。假设我已经在 Emacs 的缓冲区中启动了 IPython shell(例如使用 Python-mode.el 和 IPython.el),有没有办法选择区域在不同的缓冲区中并将该区域“发送”到已经启动的 IPython shell?
我尝试过Cc Cc
(send-buffer-to-shell)和Cc |
(send-region-to-shell),但是这只有效因为代码是用 Python 编写的,而不是用 IPython 编写的(IPython 可以运行 Python 代码)。原因似乎是,对于这两个命令,Emacs 都会创建一个带有 .py
扩展名的临时文件(而不是带有 .ipy
扩展名),然后由 IPython 解释该文件作为“Python 特定代码”。这使我无法使用 IPython 特定的功能 比如魔法命令。
另外,我还了解到 Emacs 提供了 M-| ('shell-command-on-region')
在 shell 中运行选定的区域。为了使用 IPython 解释器执行此操作,我尝试将 shell-file-name 设置为我的 IPython 路径。但是,当我选择区域后运行 M-|
时,Emacs 会提示我以下内容:
区域上的 Shell 命令:
如果我输入 RET
,我会在 *Shell Command Output*
缓冲区上获得 IPython 手册页,但不会执行该区域。是否有任何特定于 IPython 的命令可用于 M-| ('shell-command-on-region')
让 IPython 运行我的代码?
谢谢!
Update: The question still lacks a satisfactory answer.
I would like to "send" code snippets to a IPython interpreter in Emacs 23.2 (Linux). Assuming that I have already started an IPython shell in a buffer in Emacs (e.g. using Python-mode.el
and IPython.el
), is there a way of selecting a region in a different buffer and "sending" this region to the already-started IPython shell?
I have tried C-c C-c
(send-buffer-to-shell) and C-c |
(send-region-to-shell), but this only works as long as the code is written in Python and not in IPython (IPython can run Python code). The reason seems to be that, for both commands, Emacs creates a temporary file with .py
extension (as opposed to with .ipy
extension), which then is interpreted by IPython as "Python-specific code". This prevents me from using IPython-specific features
such as magic commands.
On a separate note, I have also read that Emacs provides M-| ('shell-command-on-region')
to run selected regions in a shell. To do this with an IPython interpreter, I have tried setting shell-file-name
to my IPython path. However, when I run M-|
after selecting a region, Emacs prompts me the following:
Shell command on region:
and if I then type RET
, I get the IPython man page on the *Shell Command Output*
buffer, without the region being executed. Is there any IPython-specific command that I can use for M-| ('shell-command-on-region')
to get IPython run my code?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你在使用 ipython.el 吗?
它应该可以在 Emacs 23 上正常工作。
如果不能,请查看 EmacsWiki: iPython Emacs 23。
Emacs 上的 IPython Cookbook 中还有一节(搜索 IPythonEmacs23;抱歉,不允许发布多个链接 :(
一旦您启动了 ipython-shell(抄送!),您可以从任何区域执行代码片段)在这个已经启动的 shell 中使用“执行区域(”Cc |”)缓冲
Andreas
Are you using ipython.el?
It should work okay with Emacs 23.
If not, have a look at EmacsWiki: iPython Emacs 23.
There is also a section in the IPython Cookbook on Emacs (search for IPythonEmacs23; sorry am not allowed to post more than one link :(
Once you've started an ipython-shell (C-c !) you can execute code snippets from regions of any buffer in this already started shell using "Execute region ("C-c |")
Andreas
我找到了Q1的部分答案:
Python模式提供了
Cc Cc
,它可以将缓冲区发送到已经打开的Python shell(类似Cc C-|< /code> 可以将区域发送到 shell),如果安装了 ipython.el,则默认 python shell 将设置为 IPython。
不幸的是,这仅适用于 python 脚本,不适用于 IPython 脚本。
Cc Cc
的工作原理是将缓冲区和代码片段复制到扩展名为 .py 的临时文件中,然后将该文件发送到 shell。由于该文件的扩展名是 .py,IPython 会像常规 Python 代码一样执行它,因此代码片段不能包含 IPython 特定的代码(例如 IPython 魔法命令)。I found a partial answer to Q1:
Python-mode provides
C-c C-c
which can send a buffer to an already-opened Python shell (similarlyC-c C-|
can send a region to the shell), and ifipython.el
is installed, then default python shell is set to IPython.Unfortunately, this only works for python scripts, and not for IPython scripts.
C-c C-c
works by copying the buffer with the code snippet to a temporary file with extension .py that is then sent to the shell. Since the file has extension .py IPython executes it as if it was regular Python code, and therefore the code snippet cannot have IPython-specific code (such as IPython magic commands)..