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
2
  • 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 9, 2016 at 14:09
  • 2
    For reference, upstream bug is #25306
    – npostavs
    Feb 24, 2017 at 18:47

4 Answers 4

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

13
  • 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, 2016 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. Jul 12, 2016 at 23:58
  • 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. Jul 17, 2016 at 2: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, 2016 at 3:42
  • 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, 2017 at 20:06
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

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.

4

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.