IPython无法自动补全且因jedi报TypeError而退出问题解决

ipython 7.19.0的自动补全失效,且回车后出现大段报错,提示jedi中TypeError: __init__() got an unexpected keyword argument 'column'。本文对问题进行排查并给出解决方案。

IPython无法自动补全且因jedi报TypeError而退出问题解决

1 问题描述

使用最新版的IPython 7.19.0时,发现无法Tab自动补全,且回车后会出现报错,具体情境例如:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
(pytorch) shenjiayun@server3 ~/Dev/VisualEntailment $ ipython
Python 3.7.9 (default, Aug 31 2020, 12:42:55)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.19.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: import torch

In [2]: x = torch.rand([4, 36, 2048])
Traceback (most recent call last):
File "/home/shenjiayun/miniconda3/envs/pytorch/lib/python3.7/site-packages/IPython/terminal/ptutils.py", line 113, in get_completions
yield from self._get_completions(body, offset, cursor_position, self.ipy_completer)
File "/home/shenjiayun/miniconda3/envs/pytorch/lib/python3.7/site-packages/IPython/terminal/ptutils.py", line 129, in _get_completions
for c in completions:
File "/home/shenjiayun/miniconda3/envs/pytorch/lib/python3.7/site-packages/IPython/core/completer.py", line 438, in _deduplicate_completions
completions = list(completions)
File "/home/shenjiayun/miniconda3/envs/pytorch/lib/python3.7/site-packages/IPython/core/completer.py", line 1818, in completions
for c in self._completions(text, offset, _timeout=self.jedi_compute_type_timeout/1000):
File "/home/shenjiayun/miniconda3/envs/pytorch/lib/python3.7/site-packages/IPython/core/completer.py", line 1862, in _completions
full_text=full_text, cursor_line=cursor_line, cursor_pos=cursor_column)
File "/home/shenjiayun/miniconda3/envs/pytorch/lib/python3.7/site-packages/IPython/core/completer.py", line 2030, in _complete
cursor_pos, cursor_line, full_text)
File "/home/shenjiayun/miniconda3/envs/pytorch/lib/python3.7/site-packages/IPython/core/completer.py", line 1374, in _jedi_matches
text[:offset], namespaces, column=cursor_column, line=cursor_line + 1)
File "/home/shenjiayun/miniconda3/envs/pytorch/lib/python3.7/site-packages/jedi/api/__init__.py", line 726, in __init__
project=Project(Path.cwd()), **kwds)
TypeError: __init__() got an unexpected keyword argument 'column'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/home/shenjiayun/miniconda3/envs/pytorch/bin/ipython", line 11, in <module>
sys.exit(start_ipython())
File "/home/shenjiayun/miniconda3/envs/pytorch/lib/python3.7/site-packages/IPython/__init__.py", line 126, in start_ipython
return launch_new_instance(argv=argv, **kwargs)
File "/home/shenjiayun/miniconda3/envs/pytorch/lib/python3.7/site-packages/traitlets/config/application.py", line 845, in launch_instance
app.start()
File "/home/shenjiayun/miniconda3/envs/pytorch/lib/python3.7/site-packages/IPython/terminal/ipapp.py", line 356, in start
self.shell.mainloop()
File "/home/shenjiayun/miniconda3/envs/pytorch/lib/python3.7/site-packages/IPython/terminal/interactiveshell.py", line 564, in mainloop
self.interact()
File "/home/shenjiayun/miniconda3/envs/pytorch/lib/python3.7/site-packages/IPython/terminal/interactiveshell.py", line 547, in interact
code = self.prompt_for_code()
File "/home/shenjiayun/miniconda3/envs/pytorch/lib/python3.7/site-packages/IPython/terminal/interactiveshell.py", line 475, in prompt_for_code
**self._extra_prompt_options())
File "/home/shenjiayun/miniconda3/envs/pytorch/lib/python3.7/site-packages/prompt_toolkit/shortcuts/prompt.py", line 1013, in prompt
return self.app.run(set_exception_handler=set_exception_handler)
File "/home/shenjiayun/miniconda3/envs/pytorch/lib/python3.7/site-packages/prompt_toolkit/application/application.py", line 817, in run
self.run_async(pre_run=pre_run, set_exception_handler=set_exception_handler)
File "/home/shenjiayun/miniconda3/envs/pytorch/lib/python3.7/asyncio/base_events.py", line 587, in run_until_complete
return future.result()
File "/home/shenjiayun/miniconda3/envs/pytorch/lib/python3.7/site-packages/prompt_toolkit/application/application.py", line 783, in run_async
return await _run_async2()
File "/home/shenjiayun/miniconda3/envs/pytorch/lib/python3.7/site-packages/prompt_toolkit/application/application.py", line 771, in _run_async2
await self.cancel_and_wait_for_background_tasks()
File "/home/shenjiayun/miniconda3/envs/pytorch/lib/python3.7/site-packages/prompt_toolkit/application/application.py", line 872, in cancel_and_wait_for_background_tasks
await task
File "/home/shenjiayun/miniconda3/envs/pytorch/lib/python3.7/site-packages/prompt_toolkit/buffer.py", line 1854, in new_coroutine
await coroutine(*a, **kw)
File "/home/shenjiayun/miniconda3/envs/pytorch/lib/python3.7/site-packages/prompt_toolkit/buffer.py", line 1684, in async_completer
document, complete_event
File "/home/shenjiayun/miniconda3/envs/pytorch/lib/python3.7/site-packages/prompt_toolkit/completion/base.py", line 270, in get_completions_async
document, complete_event
File "/home/shenjiayun/miniconda3/envs/pytorch/lib/python3.7/site-packages/prompt_toolkit/completion/base.py", line 196, in get_completions_async
for item in self.get_completions(document, complete_event):
File "/home/shenjiayun/miniconda3/envs/pytorch/lib/python3.7/site-packages/IPython/terminal/ptutils.py", line 116, in get_completions
exc_type, exc_value, exc_tb = sys.exc_info()
NameError: name 'sys' is not defined

If you suspect this is an IPython 7.19.0 bug, please report it at:
https://github.com/ipython/ipython/issues
or send an email to the mailing list at ipython-dev@python.org

You can print a more detailed traceback right now with "%tb", or use "%debug"
to interactively debug it.

Extra-detailed tracebacks for bug-reporting purposes can be enabled via:
%config Application.verbose_crash=True

2 问题排查

根据常识推断,ipython使用的是jedi作为language sever实现代码自动补全。当自动补全失灵的时候,那应该和jedi有关。

从长段的报错中,能看到错误定位于jedi/api/__init__.py", line 726, in __init__,提示TypeError: __init__() got an unexpected keyword argument 'column'。确实是调用jedi时出现了错误。

由此查jedi的开源项目,发现issue:

IPython (<=7.19) incompatible with jedi 0.18.0 #1714

Relevant traceback reads as follows:

1
2
3
4
5
6
7
  File "../venv/lib/python3.8/site-packages/IPython/core/completer.py", line 2029, in _complete
completions = self._jedi_matches(
File "../venv/lib/python3.8/site-packages/IPython/core/completer.py", line 1373, in _jedi_matches
interpreter = jedi.Interpreter(
File "../venv/lib/python3.8/site-packages/jedi/api/__init__.py", line 725, in __init__
super().__init__(code, environment=environment,
TypeError: __init__() got an unexpected keyword argument 'column'

经过确认,jedi所有者表示该问题系ipython作为下游应用的调用问题,待下游应用更新解决。

davidhalter commented 6 days ago

I think we should continue the discussion in ipython/ipython#12740.IMO this is an downstream issue and they should just do a new release.

3 解决方案

既然ipython目前最新的7.19.0版本无法正确调用最新的jedi 0.18.0版本,那就把jedi版本降级到0.17即可。

通过conda检索可用的jedi版本:

1
conda search jedi

可见:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
(pytorch) C:\Users\jyshen>conda search jedi
Loading channels: done
# Name Version Build Channel
jedi 0.8.1 py26_0 pkgs/free
jedi 0.8.1 py27_0 pkgs/free
jedi 0.8.1 py33_0 pkgs/free
jedi 0.8.1 py34_0 pkgs/free
jedi 0.9.0 py26_0 pkgs/free
jedi 0.9.0 py27_0 pkgs/free
jedi 0.9.0 py27_1 pkgs/free
jedi 0.9.0 py33_0 pkgs/free
jedi 0.9.0 py34_0 pkgs/free
jedi 0.9.0 py34_1 pkgs/free
jedi 0.9.0 py35_0 pkgs/free
jedi 0.9.0 py35_1 pkgs/free
jedi 0.9.0 py36_1 pkgs/free
jedi 0.10.2 py27_0 pkgs/free
jedi 0.10.2 py27_2 pkgs/free
jedi 0.10.2 py27h4f12af3_0 pkgs/main
jedi 0.10.2 py35_0 pkgs/free
jedi 0.10.2 py35_2 pkgs/free
jedi 0.10.2 py35h3350e2d_0 pkgs/main
jedi 0.10.2 py36_0 pkgs/free
jedi 0.10.2 py36_2 pkgs/free
jedi 0.10.2 py36hed927a0_0 pkgs/main
jedi 0.11.0 py27_1 pkgs/main
jedi 0.11.0 py27_2 pkgs/main
jedi 0.11.0 py27h53c0d9b_0 pkgs/main
jedi 0.11.0 py35_1 pkgs/main
jedi 0.11.0 py35_2 pkgs/main
jedi 0.11.0 py35hc856aec_0 pkgs/main
jedi 0.11.0 py36_1 pkgs/main
jedi 0.11.0 py36_2 pkgs/main
jedi 0.11.0 py36hc338079_0 pkgs/main
jedi 0.11.1 py27_0 pkgs/main
jedi 0.11.1 py27_1 pkgs/main
jedi 0.11.1 py35_0 pkgs/main
jedi 0.11.1 py35_1 pkgs/main
jedi 0.11.1 py36_0 pkgs/main
jedi 0.11.1 py36_1 pkgs/main
jedi 0.12.0 py27_0 pkgs/main
jedi 0.12.0 py27_1 pkgs/main
jedi 0.12.0 py35_0 pkgs/main
jedi 0.12.0 py35_1 pkgs/main
jedi 0.12.0 py36_0 pkgs/main
jedi 0.12.0 py36_1 pkgs/main
jedi 0.12.0 py37_1 pkgs/main
jedi 0.12.1 py27_0 pkgs/main
jedi 0.12.1 py35_0 pkgs/main
jedi 0.12.1 py36_0 pkgs/main
jedi 0.12.1 py37_0 pkgs/main
jedi 0.13.1 py27_0 pkgs/main
jedi 0.13.1 py36_0 pkgs/main
jedi 0.13.1 py37_0 pkgs/main
jedi 0.13.2 py27_0 pkgs/main
jedi 0.13.2 py36_0 pkgs/main
jedi 0.13.2 py37_0 pkgs/main
jedi 0.13.3 py27_0 pkgs/main
jedi 0.13.3 py36_0 pkgs/main
jedi 0.13.3 py37_0 pkgs/main
jedi 0.14.1 py27_0 pkgs/main
jedi 0.14.1 py36_0 pkgs/main
jedi 0.14.1 py37_0 pkgs/main
jedi 0.14.1 py38_0 pkgs/main
jedi 0.15.1 py27_0 pkgs/main
jedi 0.15.1 py36_0 pkgs/main
jedi 0.15.1 py37_0 pkgs/main
jedi 0.15.1 py38_0 pkgs/main
jedi 0.15.2 py27_0 pkgs/main
jedi 0.15.2 py36_0 pkgs/main
jedi 0.15.2 py37_0 pkgs/main
jedi 0.15.2 py38_0 pkgs/main
jedi 0.16.0 py36_0 pkgs/main
jedi 0.16.0 py36_1 pkgs/main
jedi 0.16.0 py37_0 pkgs/main
jedi 0.16.0 py37_1 pkgs/main
jedi 0.16.0 py38_0 pkgs/main
jedi 0.16.0 py38_1 pkgs/main
jedi 0.17.0 py36_0 pkgs/main
jedi 0.17.0 py37_0 pkgs/main
jedi 0.17.0 py38_0 pkgs/main
jedi 0.17.1 py36_0 pkgs/main
jedi 0.17.1 py37_0 pkgs/main
jedi 0.17.1 py38_0 pkgs/main
jedi 0.17.2 py36_0 pkgs/main
jedi 0.17.2 py36haa95532_1 pkgs/main
jedi 0.17.2 py37_0 pkgs/main
jedi 0.17.2 py37haa95532_1 pkgs/main
jedi 0.17.2 py38_0 pkgs/main
jedi 0.17.2 py38haa95532_1 pkgs/main
jedi 0.17.2 py39haa95532_1 pkgs/main
jedi 0.18.0 py36haa95532_0 pkgs/main
jedi 0.18.0 py37haa95532_0 pkgs/main
jedi 0.18.0 py38haa95532_0 pkgs/main
jedi 0.18.0 py39haa95532_0 pkgs/main

通过conda安装指定版本的jedi:

1
conda install jedi=0.17

再次测试ipython,不再出现该问题。