Vim Python completion

16,118

Solution 1

pythoncomplete is rather old and unmaintained.

Try to use Jedi: https://github.com/davidhalter/jedi-vim It was originally an improved pythoncomplete, but is now much much more powerful!

It works for complex code:completion

And has additional features: enter image description here

There is a list of all possible features:

  • builtin functions/classes support
  • complex module / function / class structures
  • ignores syntax and indentation errors
  • multiple returns / yields
  • tuple assignments / array indexing / dictionary indexing
  • exceptions / with-statement
  • *args / **kwargs
  • decorators
  • descriptors -> property / staticmethod / classmethod
  • closures
  • generators (yield statement) / iterators
  • support for some magic methods: __call__, __iter__, __next__, __get__, __getitem__, __init__
  • support for list.append, set.add, list.extend, etc.
  • (nested) list comprehensions / ternary expressions
  • relative imports
  • getattr() / __getattr__ / __getattribute__
  • function annotations (py3k feature, are ignored right now, but being parsed. I don't know what to do with them.)
  • class decorators (py3k feature, are being ignored too, until I find a use case, that doesn't work with Jedi)
  • simple/usual sys.path modifications
  • isinstance checks for if/while/assert

Solution 2

Python, being an incredibly dynamic language, doesn't lend itself to completion. There isn't any really good completion out there. It's easier to just live without it than to fight with all its problems, IMO. That said, python-mode really is fantastic, like neoascetic said.

Share:
16,118
Goran Novosel
Author by

Goran Novosel

Updated on June 08, 2022

Comments

  • Goran Novosel
    Goran Novosel almost 2 years

    I'm having trouble with Vim and Python completion. In fact I'm totally confused how does this work.

    I have generic gvim 7.3, on windows 7 (with python/dyn) I'm using SuperTab plugin, amongst many others, some of which are python-specific, with following settings in vimrc:

    au FileType python set omnifunc=pythoncomplete#Complete
    let g:SuperTabDefaultCompletionType = "context"
    let g:SuperTabContextDefaultCompletionType = "<c-n>"
    

    I did not set PYTHONPATH enviroment varariable.

    Completion works ok for system modules.

    At first I thought that it isn't working at all for non-system code, but that's not the case. What is happening is best shown on following code:

    import numpy.random   # if this line is commented completion in last line works
    
    class C(object):
        def __init__(self, x_):
            self.x=x_
    
        def getX(self):
            return self.x
    
        def pr(self):
            print 'ok'
    
    a=C(10)  # nothing changes if I put C() instead, even though it would be wrong
    a.  # here is completion in question
    

    Problem is that completion works (a.<tab> suggests getX and pr) if import line is commented. But it there is import numpy.random, completion brakes down. Note: this import works normally when I run the code.

    What are prerequisites for Python completion? What's happening and what should I do to get completion working for Python.

    As I am (relatively) new to Vim, any suggestion is appreciated.

    EDIT: It seems that the problem is in using a.b form in import. If I do from numpy import random, everything is ok. If this is reasonably easy to fix I would like to get a.b from to work too. But now that I know how to go around it that's not so important.

    Are there more unusual problem like this one so that I know what's happening in the future?