Vim plugin for automatically generating Python import statements (without using Rope)
Asked Answered
L

3

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.

Leanneleanor answered 11/4, 2015 at 16:36 Comment(3)
What’s wrong with rope? It is under maintenance again, and one echo .ropeproject/ >>.git/info/exclude can cure all your problems, cannot it?Bret
Okay there's lyz-code.github.io/autoimport/#alternatives , writing a vim plugin to do that should not be too hardScrogan
@Scrogan 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.Leanneleanor
B
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.

Berar answered 16/10, 2018 at 19:19 Comment(5)
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.Leanneleanor
Use isort fixer with ale (recommended) or isort plugin to combine multiple imports. Report bug for first problem (or maybe even fix it).Berar
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.Scrogan
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. ☺Kapoor
I guess 2023/2024 answer would be neovim with LSP and pyright, but I don't want to write separate comment for that :-)Berar
I
1

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

Note that it requires pyflakes.

Inspired answered 11/4, 2015 at 18:12 Comment(3)
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?Leanneleanor
I've never felt the need for it, so I haven't tried it.Inspired
OK. Thanks for the lead on it, in any event.Leanneleanor
S
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.)

Scharf answered 15/3, 2022 at 17:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.