Is there a Python auto import extension/plugin available for VSCode?
By auto import I mean, auto import of python modules. Eclipse and Intellij has this feature with Java.
Is there a Python auto import extension/plugin available for VSCode?
By auto import I mean, auto import of python modules. Eclipse and Intellij has this feature with Java.
VSCode team recently released Pylance
Features
- Docstrings
- Signature help, with type information
- Parameter suggestions
- Code completion
- Auto-imports (as well as add and remove import code actions)
- As-you-type reporting of code errors and warnings (diagnostics)
- Code outline
- Code navigation
- Code lens (references/implementations)
- Type checking mode
- Native multi-root workspace support
- IntelliCode compatibility
- Jupyter Notebooks compatibility
No, but it will soon be a part of vscode-python: https://github.com/Microsoft/vscode-python/pull/636
EDIT: See answer by @Eric, who built such an extension.
EDIT 2: See answer by @Eyal Levin, mentioning such an extension (Pylance).
(Updated answer as of August 2023)
These did it for me:
"python.analysis.indexing": true,
"python.analysis.autoImportCompletions": true,
If that is slowing down your computer too much because it's indexing too many files, then look into specifying patterns and depths of directories to include in the indexing using "python.analysis.packageIndexDepths"
, or using "python.analysis.exclude"
.
Note that I am using Pylance (currently the default, as of January 2023).
Check out the VSCode python settings reference for more info on each of those settings.
Edit August 2023: removed "python.analysis.autoImportUserSymbols"
because @YellowStrawHatter pointed out that no longer exists.
I have built an automatic import extension that supports Python. It lets you fully customize how the imports get written to the file, modifying import paths, names, sorting relative to other imports. The Python plugin even lets you "group" imports together with extra line breaks.
From https://github.com/microsoft/python-language-server/issues/19#issuecomment-587303061:
For those who wonder how to trigger auto-importing as I did, here are the steps.
- Enable Microsoft Python Language Server by removing the check of
Python: Jedi Enabled
in your settings.- Reload the VSCode window.
- Hover your mouse over the variable that you want to import, and click
Quick fix...
For the last step, if it shows
No quick fixes available
orChecking for quick fixes
, you may need to wait for a while until the extension has finished code analysis. It is also possible to set a shortcut that triggers a quick fix.
This is supported in the official Microsoft python extension, but for some reason I found it was recently disabled or no longer default. The setting I had to toggle was
"python.analysis.autoImportCompletions": true,
I use this package it works very well
https://marketplace.visualstudio.com/items?itemName=codeavecjonathan.importmagic
You can find it in VSCode extension store. it's name is IMPORTMAGIC. It works fantastic. It will include all modules which you use in your script.
It has code action ctrl + . , which will also import library.
You can set the setting(true) below to settings.json
for auto import. *Pylance extension installed automatically when Python extension is installed has the setting(true) below which is false
by default and you can see my answer explaning how to open settings.json
:
// "settings.json"
{
...
"python.analysis.autoImportCompletions": true
}
Then, it shows all matched attributes and modules as shown below:
Then, pressing Enter can automatically import what you select as shown below:
In addition, if you don't set the setting(true) below to settings.json
for auto import:
// "settings.json"
{
...
// "python.analysis.autoImportCompletions": true
}
Then, it only shows below:
You might find This Python-Based Module useful if you wish to auto import and auto download missing modules from a script or sub-scripts. Not only for VSCode, but also for any IDE or Editor.