Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IPython in Emacs. Quick code evaluation

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!

like image 291
Amelio Vazquez-Reina Avatar asked Dec 27 '10 23:12

Amelio Vazquez-Reina


1 Answers

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

like image 145
Andreas Avatar answered Oct 15 '22 06:10

Andreas