8

I've seen similar questions asked before here and here, however they are 4 years old and did not yield answers that matched my requirements.

If I type Python code into Vim, for example:

os.path.join('my', 'path')
resp = requests.get('http://example.com')
HttpResponse('success')

Assuming that I had the third-party modules 'requests' and 'django' in my site-packages folder, is there any Vim plugin -- which does not use the Rope library -- that could automatically add the relevant import statements to the Python file (both for built-in & third-party modules, using either import or from as needed), like this:

import os
import requests
from django.http import HttpResponse

While I would traditionally use the venerable Rope package, I have been replacing Rope functions with modern alternatives to avoid the overhead of the .ropeproject folder. However, I haven't found a Vim alternative yet for auto-import.

3
  • 1
    What’s wrong with rope? It is under maintenance again, and one echo .ropeproject/ >>.git/info/exclude can cure all your problems, cannot it? Commented Oct 19, 2017 at 10:23
  • Okay there's lyz-code.github.io/autoimport/#alternatives , writing a vim plugin to do that should not be too hard Commented Dec 7, 2021 at 15:30
  • @user202729 There are thankfully now (6 years after I posted the question) some great solutions. For example, Neovim 0.5 (having built-in LSP support) paired with nvim-cmp and pyright can automatically add import statements when auto-completing a class or function name. Which is fantastic. Now if only auto-importing understood when it should import from parent modules rather than always from the bottom-most level, I'd be living the dream. Commented Dec 9, 2021 at 7:39

3 Answers 3

6

I am using python-imports.vim (https://github.com/mgedmin/python-imports.vim). Combine that with gutentags plugin and it is almost perfect. Perfect solution would create all imports and magically guess correct modules in case of name collision but I doubt that this is possible.

Update 2022-05-14: Another way is to use Ale (https://github.com/dense-analysis/ale) with pyright (https://github.com/microsoft/pyright) and :ALEImport command.

Sign up to request clarification or add additional context in comments.

5 Comments

Some other caveats I noticed with this one is it will add the import from a submodule even when it's available from a parent module, and also it doesn't combine multiple imports from the same module on a single line, but overall this plugin accomplishes this task very well. Great find.
Use isort fixer with ale (recommended) or isort plugin to combine multiple imports. Report bug for first problem (or maybe even fix it).
I think this solution (as well as the vimpy solution below), requires the user to put the cursor at the identifier instead of go through the file automatically, unlike (supposedly) the solutions using ropevim or mr.igor.
I landed here after websearching for the 'autoimport' that :ALEFixSuggest recommends—I'm new to ALE (I've been using Syntastic for years, but it's deprecated now), so it's not clear to me exactly what it was referring to. Anyhow, I think you should've written a separate answer, instead of adding the update, but if I end up up-voting this answer, it'll be for that. ☺
I guess 2023/2024 answer would be neovim with LSP and pyright, but I don't want to write separate comment for that :-)
1

Have a look at vimpy. From its description it does what you are looking for.

Note that it requires pyflakes.

3 Comments

I've been trying to get vimpy working this week, but it produces errors. I submitted a bug report on GitHub, but the maintainer no longer seems to actively maintain the project. Last commit was over a year ago. Have you successfully used this project?
I've never felt the need for it, so I haven't tried it.
OK. Thanks for the lead on it, in any event.
0

Very late to the party but since this question exists, I might as well post a new answer.

I use coc-python for everything of this nature.

It just works like magic:

enter image description here

The only caveat is that you have to press Ctrl-y to accept the Auto-import suggestion.

Make sure you haven't got Ctrl-y mapped to anything else in insert mode (a common plugin called Emmet remaps this combination, for example.)

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.