messy code when using emacs ipython Inferior Python

1k Views Asked by At

I am using the newest emacs25.1.1 with the newest archlinux.

However, when I edit a python scripts and send it to the Inferior ipython by using Ctrl-Enter, the inferior Python show me a ugly messy code termial indicater like:

Python 3.5.2 (default, Nov  7 2016, 11:31:36) 
Type "copyright", "credits" or "license" for more information.

IPython 5.1.0 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

[JIn [1]: import numpy as np[26D
[J
[6n[JIn [2]: [8D[8C[8D[J[6n[JIn [2]: [8D[8C

When press Enter in this Inferior python, it shows:

error in process filter: Args out of range: "
^[[6n^[[0m^[[0m^[[J^[[0;38;5;28mIn [^[[0;38;5;28m: ^[[8D^[[8C[[0m^[[0m", 128

I change the emacs locale from:

LANG=zh_CN.UTF-8
LC_CTYPE="zh_CN.UTF-8"
LC_NUMERIC=en_US.UTF-8
LC_TIME=en_US.UTF-8
LC_COLLATE="zh_CN.UTF-8"
LC_MONETARY=en_US.UTF-8
LC_MESSAGES="zh_CN.UTF-8"
LC_PAPER=en_US.UTF-8
LC_NAME="zh_CN.UTF-8"
LC_ADDRESS="zh_CN.UTF-8"
LC_TELEPHONE="zh_CN.UTF-8"
LC_MEASUREMENT=en_US.UTF-8
LC_IDENTIFICATION="zh_CN.UTF-8"
LC_ALL=

To:

LANG=en_US.UTF-8
LC_CTYPE="en_US.UTF-8"
LC_NUMERIC=en_US.UTF-8
LC_TIME=en_US.UTF-8
LC_COLLATE="en_US.UTF-8"
LC_MONETARY=en_US.UTF-8
LC_MESSAGES="en_US.UTF-8"
LC_PAPER=en_US.UTF-8
LC_NAME="en_US.UTF-8"
LC_ADDRESS="en_US.UTF-8"
LC_TELEPHONE="en_US.UTF-8"
LC_MEASUREMENT=en_US.UTF-8
LC_IDENTIFICATION="en_US.UTF-8"
LC_ALL=

with the command $"export LANGUAGE="en_US:en" && emacs"$, no help to resolve the messy code.

I also try python-mode.et or elpy package in emacs, all show the same messy terminal code.

Further,python2.7 also tried, no work:

Python 2.7.12 (default, Nov  7 2016, 11:55:55) 
Type "copyright", "credits" or "license" for more information.

IPython 5.1.0 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

[JIn [1]: import numpy as np[26D
[J
[6n[JIn [2]: [8D[8C[8D[J[6n[JIn [2]: [8D[8C

Is the newest ipython problem?

At last the message in emacs paste here:

error in process filter: ansi-color-filter-apply: Args out of range: "Python 3.5.2 (default, Nov  7 2016, 11:31:36) 
Type \"copyright\", \"credits\" or \"license\" for more information.

IPython 5.1.0 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

[6n[0m
[0m[J
[0m", 408
error in process filter: Args out of range: "Python 3.5.2 (default, Nov  7 2016, 11:31:36) 
Type \"copyright\", \"credits\" or \"license\" for more information.

IPython 5.1.0 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

[6n[0m
[0m[J
[0m", 408
error in process filter: ansi-color-filter-apply: Args out of range: "[0m[0m[J[0;38;5;28mIn [[0;38;5;10;1m1[0;38;5;28m]: [0;38;5;28;1mimport[0m [0;38;5;32;1mnumpy[0m [0;38;5;28;1mas[0m [0;38;5;32;1mnp[26D[0m
[J[0m", 177
error in process filter: Args out of range: "[0m[0m[J[0;38;5;28mIn [[0;38;5;10;1m1[0;38;5;28m]: [0;38;5;28;1mimport[0m [0;38;5;32;1mnumpy[0m [0;38;5;28;1mas[0m [0;38;5;32;1mnp[26D[0m
[J[0m", 177
error in process sentinel: elpy-rpc--default-error-callback: peculiar error: "exited abnormally with code 1"
error in process sentinel: peculiar error: "exited abnormally with code 1"
error in process filter: ansi-color-filter-apply: Args out of range: #("[JIn [1]: import numpy as np[26D
[J


[6n[0m[0m[J[0;38;5;28mIn [[0;38;5;10;1m2[0;38;5;28m]: [8D[8C[0m[0m[8D[J[0m[6n[0m[0m[J[0;38;5;28mIn [[0;38;5;10;1m2[0;38;5;28m]: [8D[8C[0m" 0 38 (fontified nil) 38 39 (fontified nil)), 283
error in process filter: Args out of range: #("[JIn [1]: import numpy as np[26D
[J


[6n[0m[0m[J[0;38;5;28mIn [[0;38;5;10;1m2[0;38;5;28m]: [8D[8C[0m[0m[8D[J[0m[6n[0m[0m[J[0;38;5;28mIn [[0;38;5;10;1m2[0;38;5;28m]: [8D[8C[0m" 0 38 (fontified nil) 38 39 (fontified nil)), 283
2

There are 2 best solutions below

4
On

From the output and error message, it would appear that for some reason, your setup is not handling the ansi escape sequences used by ipython. Assumiing you are using pythong-mode, ensure you have the following

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

To eliminate other possible settings in your init file, I would comment out everything except the above and try again. If it works, then there is something in your setup which is messing with the ability of emacs to handle the ansi control sequences being output by ipython. If it still doesn't work, then you may need to log a bug report.

there are also a few things you can do to work around the issue.

  1. Try adding the --no-color-info switch to ipython. This will reduce the number of control characters ipython outputs, which my limit the problem.
  2. Consider using just python rather than ipython as the inferior process. The problem you are seeing is due to ipython using ansi control characters to provide a nicer shell. However, emacs is really your shell and you may be better off letting emacs take care of the interface and just use a simpler inferior shell
  3. The Emacs Wiki has a fairly detailed description on how to setup a powerful python development environment. Using a simple ipython/python inferior process is the most basic approach. You may do better putting in time with some of the other packages to get a more complete development environment.
0
On

I've used recomendation from https://github.com/jorgenschaefer/elpy/issues/992#issuecomment-249165923. In short - add the following code: (setenv "IPY_TEST_SIMPLE_PROMPT" "1") into emacs configuration file.