Python – Jedi-vim autocomplete python 3.6 virtualenv does not work

Jedi-vim autocomplete python 3.6 virtualenv does not work… here is a solution to the problem.

Jedi-vim autocomplete python 3.6 virtualenv does not work

I’ve been following some issues on jedi-vim repo and I’ve found:
https://github.com/davidhalter/jedi-vim/issues/704 sum https://github.com/davidhalter/jedi/pull/829/files .

I

tried to rename the library in my venv to python 3.5, yes, autocomplete works fine, but when you run any python file, it breaks (I mean I changed the name, so this is fine).

For other solutions, I can’t find any file named jedi/evaluate/sys_path.py in my vundle directory.

Does anyone have an idea to make this work, I’ve searched for a long time but can’t find anything.

Thanks in advance

Solution

After a long period of use, my pyenv-virtualenv, vim, and jedi settings got it working. Hope that helps.

First, I added the jedi-vim plugin to the Vundle block of the ~/.vimrc file:

set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()
...
Plugin 'davidhalter/jedi-vim' 
call vundle#end()            

Next, I added the following python3 code in ~/.vimrc to search and add packages from the pyenv/virtualenv directory, respectively. Unfortunately, there is no activate_this.py script that can do this automatically

  py3 << EOF
  import os.path
  import sys
  import vim
  import jedi
  if 'VIRTUAL_ENV' in os.environ:
      base = os.environ['VIRTUAL_ENV']
      site_packages = os.path.join(base, 'lib', 'python%s' %  sys.version[:3], 'site-packages')
      prev_sys_path = list(sys.path)
      import site
      site.addsitedir(site_packages)
      sys.real_prefix = sys.prefix
      sys.prefix = base
      # Move the added items to the front of the path:
      new_sys_path = []
      for item in list(sys.path):
          if item not in prev_sys_path:
          new_sys_path.append(item)
          sys.path.remove(item)
      sys.path[:0] = new_sys_path
   EOF

Make sure you can run import jedi and import vim in native Python. You can install them in Terminal using the following command:

pip3 -install jedi and 
pip3 -install vim

Finally, I set the following values in my vimrc file:

set omnifunc=jedi#completions
let g:jedi#force_py_version = '3'

Make sure to switch to the pyenv environment using pyenv activate before starting vim. Only after that autocomplete will it work.

Related Problems and Solutions