iPython Code Autocompletion

Posted on Fri 01 July 2022 in Python

Introduction

Today a tried to use ipython when this error wildly appeared:

In [1]: import glob

Unhandled exception in event loop:
  File "/home/auraham/.virtualenvs/dev/lib/python3.8/site-packages/prompt_toolkit/eventloop/coroutine.py", line 92, in step_next
    new_f = coroutine.throw(exc)
  File "/home/auraham/.virtualenvs/dev/lib/python3.8/site-packages/prompt_toolkit/buffer.py", line 1654, in new_coroutine
    yield From(coroutine(*a, **kw))
  File "/home/auraham/.virtualenvs/dev/lib/python3.8/site-packages/prompt_toolkit/eventloop/coroutine.py", line 92, in step_next
    new_f = coroutine.throw(exc)
  File "/home/auraham/.virtualenvs/dev/lib/python3.8/site-packages/prompt_toolkit/buffer.py", line 1503, in async_completer
    yield From(consume_async_generator(
  File "/home/auraham/.virtualenvs/dev/lib/python3.8/site-packages/prompt_toolkit/eventloop/coroutine.py", line 88, in step_next
    new_f = coroutine.send(None)
  File "/home/auraham/.virtualenvs/dev/lib/python3.8/site-packages/prompt_toolkit/eventloop/async_generator.py", line 117, in consume_async_generator
    item = iterator.send(send)
  File "/home/auraham/.virtualenvs/dev/lib/python3.8/site-packages/prompt_toolkit/completion/base.py", line 176, in get_completions_async
    for item in self.get_completions(document, complete_event):
  File "/home/auraham/.virtualenvs/dev/lib/python3.8/site-packages/IPython/terminal/ptutils.py", line 90, in get_completions
    yield from self._get_completions(body, offset, cursor_position, self.ipy_completer)
  File "/home/auraham/.virtualenvs/dev/lib/python3.8/site-packages/IPython/terminal/ptutils.py", line 100, in _get_completions
    for c in completions:
  File "/home/auraham/.virtualenvs/dev/lib/python3.8/site-packages/IPython/core/completer.py", line 445, in _deduplicate_completions
    completions = list(completions)
  File "/home/auraham/.virtualenvs/dev/lib/python3.8/site-packages/IPython/core/completer.py", line 1820, in completions
    for c in self._completions(text, offset, _timeout=self.jedi_compute_type_timeout/1000):
  File "/home/auraham/.virtualenvs/dev/lib/python3.8/site-packages/IPython/core/completer.py", line 1863, in _completions
    matched_text, matches, matches_origin, jedi_matches = self._complete(
  File "/home/auraham/.virtualenvs/dev/lib/python3.8/site-packages/IPython/core/completer.py", line 2031, in _complete
    completions = self._jedi_matches(
  File "/home/auraham/.virtualenvs/dev/lib/python3.8/site-packages/IPython/core/completer.py", line 1374, in _jedi_matches
    interpreter = jedi.Interpreter(
  File "/home/auraham/.virtualenvs/dev/lib/python3.8/site-packages/jedi/api/__init__.py", line 729, in __init__
    super().__init__(code, environment=environment, project=project, **kwds)

Exception __init__() got an unexpected keyword argument 'column'
Press ENTER to continue...

It happened after trying to autocomplete glob.iglob, that is, after pressing Tab. If you inspect the stacktrace, the issue seems to be in the jedi package. After a quick search, I found these solutions:

Downgrade jedi

pip install jedi==0.17.2

Downgrade prompt-toolkit

python -m pip install -U prompt-toolkit~=2.0

Just for the record, the versions of these packages in my virtual environment are:

 23:29:51 [crafting] auraham@rocket ~ 
 > pip freeze|egrep "jedi|prompt"
jedi==0.18.1
prompt-toolkit==3.0.30

So, let's downgrade both packages, just to be sure:

pip install jedi==0.17.2
pip install prompt-toolkit~=3.0

I tried to use prompt-toolkit~=2.0, but it just thrown another issue, so I preferred the 3.0 version.

After that, I can now use autocompletion without any fear on ipython.

For completeness, these are the versions that fixed the autocompletion issue in ipython:

 23:38:44 [crafting] auraham@rocket ~
 > pip freeze|egrep "ipython|jedi|prompt"
ipython==8.4.0
jedi==0.17.2
prompt-toolkit==3.0.30

and these are all packages in the virtual environment:

 23:41:07 [crafting] auraham@rocket ~
 > pip freeze
asttokens==2.0.5
backcall==0.2.0
decorator==5.1.1
executing==0.8.3
importlib-metadata==4.12.0
ipdb==0.13.9
ipython==8.4.0
jedi==0.17.2
Markdown==3.3.7
matplotlib-inline==0.1.3
parso==0.7.1
pexpect==4.8.0
pickleshare==0.7.5
prompt-toolkit==3.0.30
ptyprocess==0.7.0
pure-eval==0.2.2
Pygments==2.12.0
six==1.16.0
smartypants==2.0.1
stack-data==0.3.0
toml==0.10.2
traitlets==5.3.0
wcwidth==0.2.5
zipp==3.8.0

Update

This post recommends to install pyreadline. I have these versions and autocomplete is working:

 > pip freeze|egrep "pyread|jedi"
jedi==0.18.2
pyreadline==2.1

References