48

Does anyone know how to set up auto completion to work nicely with python, django, and vim?

I've been trying to use pysmell, but I can't seem to get it set up correctly (or maybe I don't know how it works). Right now, I run pysmell in the django directory (I'm using the trunk) and move the resulting tags to my project directory, then I also run pysmell in the project directory. Vim doesn't pick up the django tags, though, and they don't get auto completed.

Does anyone know how to set up auto completion in vim so that it will complete the long django functions (like get_object_or_404) as well as classes/functions in my own code? I have poked around on google but haven't found any good resources.

Thanks.

1

5 Answers 5

30

First off, thank you for asking this question, as it forced me to figure this out myself and it's great!

Here is the page I used as a reference: PySmell v0.6 released : orestis.gr

  1. Install PySmell using the setup.py install command.
  2. Generate the PYSMELLTAGS file for django by going to your site-packages/django directory and running: pysmell . -o ~/PYSMELLTAGS.django
  3. Copy that file to your project directory, and then ran pysmell . to generate the project PYSMELLTAGS file
  4. Make sure pysmell is in your PYTHONPATH (export PYTHONPATH=${PYTHONPATH}:/path/to/pysmell/)
  5. Run vim (vim .)
  6. Source pysmell.vim (:source /path/to/pysmell/pysmell.vim)
  7. Set the autocomplete command (:set omnifunc=pysmell#Complete)
  8. Type ^x^o to autocomplete and it should work

I realize this is not a sustainable solution, but you should be able to use this as a start to getting it setup to always work (e.g., add the export to your .bashrc, add the :source to your .vimrc, setup autocmd FileType python set omnifunc=pysmell#Complete, etc.)

Let me know if this is enough to get you started. It worked for me!

Edit I simply added this to my .vimrc and as long as the PYSMELLTAGS & PYSMELLTAGS.django files are in my project root, it works fine without any other work:

python << EOF
import os
import sys
import vim
sys.path.append("/usr/local/python/lib/python2.5/site-packages")
EOF
exe ":source ~/src/pysmell/pysmell.vim"
autocmd FileType python set omnifunc=pysmell#Complete
4
  • I pretty much did this, and can't seem to get it to work. The only difference is that pysmell.vim is in my ~/.vim/plugins directory, so I don't need to source it in the vimrc. However, it looks like auto-completion still is not working. To test it, I open a python file like test.py and do something like "from django.shortcuts import get_object_or_404", and then on the next line I type "get_" and ^x^o, and it says that it can't find anything to auto-complete. Am I using it wrong?
    – So8res
    Jun 11, 2009 at 17:00
  • Not sure if it's a typo or not, but in case it's not, I believe the plugin directory is .vim/plugin NOT .vim/pluginS. Double-check that and it might take care of your problems.
    – bchang
    Jun 11, 2009 at 17:48
  • Yeah, that was a typo on my part when I entered it here: pysmell.vim is getting sourced and it's still not working :(. Do you have to press anything (like F2) to update the tags in pysmell after you've imported something? I didn't think you had to, and I obviously tried F2 to no avail.
    – So8res
    Jun 11, 2009 at 19:48
  • 1
    Guys, it's now 4 years later. Is Pysmell still the go-to solution for vim django support?
    – user798719
    Jun 29, 2013 at 19:08
13

As I wrote in other places, I developed Jedi. I really think it is far better than all the existing solutions (even PyCharm).

https://github.com/davidhalter/jedi-vim

It is built upon pythoncomplete and 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
4
  • Awesome. You forgot to answer the question - how to install it? There's no guide on your github page too.
    – GDR
    Feb 5, 2013 at 17:44
  • It assumes that you know what pathogen is and you know how to install it.It took me some googling to set up the plugin (which by the way is great).
    – GDR
    Feb 7, 2013 at 13:15
  • I couldn't get this to work: Error: jedi-vim failed to initialize Python: :python3 import sys; print(sys.version) 3.3.2 (default, Sep 19 2013, 11:15:15) [GCC 4.4.6] I am running on a virtual env. Anything I can check?
    – Jean
    Oct 19, 2022 at 17:26
  • You are using a Python version from 2013. Embrace the new decade :) Oct 21, 2022 at 22:06
12

alt text
(source: dispatched.ch)

You can set up VIM with buffers, buffer display, auto complete, even Py Doc display.

Here you go

3

I've had good luck with exuberant-ctags for this.

I use this macro in my vimrc:

execute 'map  :!/usr/bin/exuberant-ctags -f '.&tags.' --recurse '.$_P4ROOT.' '

You'll want to modify that slightly, so that it includes your python /site-packages/django/ directory as well as your own code.

Then, hit F2 inside vim to update the tags, and use the regular vim tag bindings to navigate.

2

Today, you not need special extentions for django autocomplete in vim. Make sure that you have vim with python support. To check it, type in xterm:

vim --version|grep python

output:

+python -python3 +quickfix +reltime +rightleft -ruby +scrollbind +signs ... ...

To make work autocomplete, add this lines in your .vimrc:

autocmd FileType python set omnifunc=pythoncomplete#Complete

if has("python")

python import sys,os

python sys.path.append('/home/sergey/workspace/django')

python os.environ['DJANGO_SETTINGS_MODULE'] = 'djangoProject.settings'

endif

where:

  • sys.path.append is path to your django workspace directory
  • djangoProject is name of your django project, which is going just after '/home/sergey/workspace/django'

Finally, save it and restart vim. Now, after '.', you press default ctrl-x ctrl-o to get your autocomplete.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.