Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vim Python completion

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?

like image 858
Goran Novosel Avatar asked Aug 17 '11 18:08

Goran Novosel


People also ask

Is there autocomplete in VIM?

Vim text editor supports autocompletion for the standard text files by default. Also, when configured properly, Vim enables an autocomplete feature for files with code in the languages it recognizes.

Is VIM a good IDE for Python?

Vim (or NeoVim) is my favourite text editor / IDE. I love its extensibility, adaptability, and in-terminal slickness. Before I start, here's a quick guide on installing Neovim.


1 Answers

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
like image 153
Dave Halter Avatar answered Sep 19 '22 14:09

Dave Halter