Python auto import extension for VSCode

Question:

Is there a Python auto import extension/plugin available for Visual Studio Code?

By auto import I mean, auto import of python modules. Eclipse and Intellij has this feature with Java.

Asked By: Soumitri Pattnaik

||

Answers:

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).

Answered By: Zachary Ryan Smith

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.

Answered By: Eric

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.

  1. Enable Microsoft Python Language Server by removing the check of
    Python: Jedi Enabled in your settings.
  2. Reload the VSCode window.
  3. 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 or
Checking 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.

Answered By: Eyal Levin

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
Answered By: Eyal Levin

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.

Answered By: bhargav3vedi

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.

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,

Answered By: James

(Updated answer as of January 2023)

These three together did it for me:

  "python.analysis.autoImportCompletions": true,
  "python.analysis.autoImportUserSymbols": true,
  "python.analysis.indexing": 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".

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.

Answered By: Brendan Goggin