我以前见过类似的问题here https://stackoverflow.com/questions/3825073/is-there-a-plugin-for-vim-to-auto-import-python-libraries and here https://stackoverflow.com/questions/3838515/auto-include-python-import-statements-in-vim-emacs,但是他们已经 4 岁了,并没有给出符合我要求的答案。
例如,如果我在 Vim 中输入 Python 代码:
os.path.join('my', 'path')
resp = requests.get('http://example.com')
HttpResponse('success')
假设我的 site-packages 文件夹中有第三方模块“requests”和“django”,是否有任何 Vim 插件(不使用 Rope 库)可以自动将相关导入语句添加到 Python文件(对于内置模块和第三方模块,使用import
or from
根据需要),像这样:
import os
import requests
from django.http import HttpResponse
虽然我传统上会使用古老的 Rope 包,但我一直在用现代替代方案替换 Rope 函数,以避免 .ropeproject 文件夹的开销。然而,我还没有找到自动导入的 Vim 替代品。
我正在使用 python-imports.vim (https://github.com/mgedmin/python-imports.vim https://github.com/mgedmin/python-imports.vim)。与 gutentags 插件结合起来,它几乎是完美的。完美的解决方案将创建所有导入并在名称冲突的情况下神奇地猜测正确的模块,但我怀疑这是可能的。
2022-05-14 更新:另一种方法是使用 Ale (https://github.com/dense-analysis/ale https://github.com/dense-analysis/ale)与pyright(https://github.com/microsoft/pyright https://github.com/microsoft/pyright) 和 :ALEImport 命令。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)