41

I just upgraded to the latest version of IPython and now something is wrong when using it in emacs.

When using it in a regular terminal it works fine (colors, tab-completion, etc.), however either in M-x eshell or M-x shell, instead of outputing In [1]:, it actually outputs [?12l[?25h and Out[1] outputs to [J[?7h[?12l[?25h[?2004l[?7hOut[1]:.

Maybe it's some feature of the terminal not available on emacs used by IPython, but I'd like to know which one it is.

Terminal.app:

In [1]: 2+2
Out[1]: 4

Emacs

[?12l[?25h2+2
[J[?7h[?12l[?25h[?2004l[?7hOut[1]: 4
Literal
  • 675
  • 5
  • 10
  • Did you open a bug upstream? I couldn't find anything, but it's also a bit hard to search ATM with Gmane being reworked. – rasmus Sep 09 '16 at 14:09
  • 2
    For reference, upstream bug is [#25306](https://debbugs.gnu.org/cgi/bugreport.cgi?bug=25306) – npostavs Feb 24 '17 at 18:47

4 Answers4

48

IPython 5 has a new terminal interface, which is not compatible with Emacs' inferior shells. To fix it, add the --simple-prompt flag to the IPython command:

(setq python-shell-interpreter "ipython"
    python-shell-interpreter-args "--simple-prompt -i")

From the documentation:

The new interface is not compatible with Emacs ‘inferior-shell’ feature. To continue using this, add the --simple-prompt flag to the command Emacs runs. This flag disables most IPython features, relying on Emacs to provide things like tab completion.

Reference: http://ipython.readthedocs.io/en/stable/whatsnew/version5.html#id1

Ricardo Lima
  • 596
  • 5
  • 4
  • 6
    Yes, that's what I did at first, however the lack of multiline support (most important for function definitions) made me revert back to ipython 4. It should be noted though that ipython 5 works fine with M-x term. – Literal Jul 12 '16 at 21:55
  • 3
    Setting this flag is still very useful with Elpy, though—through the variable `python-shell-interpreter-args`. If you haven't tried Elpy, you should check it out. It allows you to write your function definitions in a separate buffer and easily pass them to the interpreter with just one command. In this case, you can make use of all of Emacs' editing commands and get an interactive session—best of both worlds. – Ricardo Lima Jul 12 '16 at 23:58
  • So @Literal you haven't found any fix for it yet, and you're still running the reverted IPython 4? (which I also did) – cjauvin Jul 16 '16 at 15:21
  • @cjauvin Yeah pretty much. I have been lead to believe the issue is color-related, ie. the terminal invoked by M-x shell or M-x eshell only supports 256 colors and IPython relies on a wider palette, I think I saw that somewhere on the github of the project. I just find it weird that M-x term will run support it fine. Anyway I'm perfectly fine with IPython 4. – Literal Jul 17 '16 at 02:22
  • @cjauvin my answer is the correct fix for it, it says right on the official documentation link I referenced. I don't know why Literal didn't accept it as the answer. – Ricardo Lima Jul 17 '16 at 02:39
  • 1
    @Literal the reason why is also written on the link: they changed from using `readline` to `prompt_toolkit` on IPython 5. `prompt_toolkit` is not compatible with Emacs shells, which work differently from `term`: as far as I know, shells run dumb terminals with the purpose of working more like a standard Emacs buffer, whereas `term` runs an actual full terminal emulator. It seems shells are compatible with `readline` but not with `prompt_toolkit`, which would explain the prompt showing the escape sequences. Since `term` runs a full terminal, it correctly interprets the escape sequences. – Ricardo Lima Jul 17 '16 at 02:45
  • 1
    I didn't accept it because I would have liked to retain the other features, perhaps using another shell mode with more features. But I guess I'll have to live with it. – Literal Jul 17 '16 at 03:42
  • worked for me in Emacs24.5. Emacs25 still not quite right, I gather that's a bigger issue – pauljohn32 Nov 16 '16 at 20:11
  • If "This flag disables most IPython features", what is the point of using IPython then, why not just use Python as the `python-shell-interpreter` – CodyChan May 04 '17 at 03:44
  • 1
    @CodyChan It disables IPython features relating to the UI, like completion, colors, etc. It keeps things like %magic commands, input/output history, !shell commands, ?help commands, etc. – jpkotta May 16 '17 at 20:06
  • 1
    Unfortunately this does not work for me in emacs 25.2.2 with python3.6 and ipython 7.5.0. I'm getting ```WARNING: your terminal doesn't support cursor position requests (CPR).``` – Ytsen de Boer Jun 26 '19 at 12:35
  • @YtsendeBoer It WORKS but had to adapt the answer. Posted as an answer below to also show the different kinds of error. See if this works for you. – Pandian Le Jun 17 '20 at 18:31
  • @Thej Kiran Thanks, good to hear. I am using pdb now, turns out I have no need of ipdb :) – Ytsen de Boer Jun 18 '20 at 20:12
1

The error I got was similar garbled (wierd) text when I opened IPYTHON with C-c C-p on my simple python file print("hello World").

ppppprrrrriiiiinnnnnttttt((((("""""HHHHHeeeeellllllllllooooo     
WWWWWooooorrrrrlllllddddd""""")))))))))))))))
Hello World

Solution verbatim as per @ricardoLima's answer above didn't work for me. I had to write it as follows.

(setq python-shell-interpreter "/path/to/bin/ipython")
(setq python-shell-interpreter-args "--simple-prompt -i")

TIP:

Just use emacs -q in the terminal to open a no-init emacs and run these commands with M-:. M-x run-python and you are good to go.

P.S.
Emacs: "This is GNU Emacs 25.3.2 (x86_64-pc-linux-gnu, GTK+ Version 3.18.9) of 2019-12-24"
Ubuntu: 16.04
python and Ipython from Anaconda
Python: 3.7.6
IPython: 7.12.0

Pandian Le
  • 260
  • 3
  • 13
1

Simply using M-x term instead of M-x shell or M-x eshell solved my similar problem. The interface is actually better than eshell or shell. For example, for repeating the previous command just need the up key; Auto-completion works smoothly (but editing multi-lines is not as nice as eshell or shell).

I should also add that, for me the solution mentioned above by Ricarod did not help (adding the --simple-prompt flag to the IPython command)

0

There's a way to open IPython in emacs shell without using --simple-prompt

1. pip install rlipython

2. Open IPython in terminal

3. Run import rlipython; rlipython.install() 

After this, you should be able to open ipython directly in emacs shell without experiencing the display problem.

There's one more issue in my case though: For some reason, even after display became correct, I still couldn't directly copy-paste some multi-line class definitions into ipython in emacs shell. My current solution is to use the Ipython %paste command. If you have better approaches, please let me know.

PS: I'm using Ubuntu 16.04. Someone uses MacOS says it breaks IPython.

DataHungry
  • 247
  • 1
  • 7