72

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.

Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129
Soumitri Pattnaik
  • 3,246
  • 4
  • 24
  • 42

10 Answers10

34

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
Eyal Levin
  • 16,271
  • 6
  • 66
  • 56
  • 14
    Note that Pylance is [closed-source and that the Microsoft Python Language Server will be deprecated in favor of Pylance](https://devblogs.microsoft.com/python/announcing-pylance-fast-feature-rich-language-support-for-python-in-visual-studio-code/). Looks like Microsoft is back to playing the good ol' game of "embrace extend extinguish". :( – balu Apr 26 '21 at 15:23
  • 3
    So how do you do auto imports with Pylance? I couldn't find it – Oleg Yablokov Feb 16 '22 at 14:35
  • 3
    Pylance is proprietary and not open source, another microsoft foot-in-the-door scam. So do not try to advertise that. DOWNVOTING this. – Amir Hossein Baghernezad Apr 04 '22 at 04:59
  • 1
    Install **Pylance** extension and try to use it. Type `dateti` in IDE VSCode python file and look if appears autocompletion with Auto-import link. Click on Auto-import, result will be auto added `from datetime import datetime`. – Romasius May 20 '22 at 12:34
  • If you get a printout of "The Zen of python" while using pylance Auto-Import Completion, it has probably added a self reference somewhere in your code. Search for "from this" or "from this import d" – neilhighley Aug 11 '22 at 15:23
  • 3
    yes, Pylance auto-imports work, you just have to enable them in the settings: python.analysis.autoImportCompletions Used to control the offering of auto-imports in completions. Accepted values: true false (default) – Rok Sprogar Nov 19 '22 at 13:54
12

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

Zachary Ryan Smith
  • 2,688
  • 1
  • 20
  • 30
  • 6
    Postponed: https://github.com/Microsoft/vscode-python/pull/636#issuecomment-424894985 – william_grisaitis Dec 02 '18 at 08:00
  • Not working for me, but with [Pylance](https://marketplace.visualstudio.com/items?itemName=ms-python.vscode-pylance) everything does. I simply ctrl+dot / cmd+dot on a new class and it allows me to add a new import. – Martin Braun Feb 11 '21 at 23:47
12

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

Brendan Goggin
  • 2,061
  • 14
  • 14
  • 1
    `autoImportUserSymbols` did it for me - thank you alot! In my case it was also required to add: `"python.autoComplete.extraPaths": ["${workspaceFolder}/scripts"]` – Daniel Eisenreich Feb 21 '23 at 08:03
  • 1
    Autoimport has been disabled by default in the November 2022 release of VS Code because of how "*annoying it can be to have an import automatically added to a file*" (that was a [Github request](https://github.com/microsoft/pylance-release/issues/2344)). On my side, I've turned it on and I rely on my linter ([ruff](https://github.com/charliermarsh/ruff)) to check for unused imports. Source : https://devblogs.microsoft.com/python/python-in-visual-studio-code-november-2022-release/#auto-imports-turned-off-by-default-with-pylance – scūriolus Apr 03 '23 at 21:17
  • Thank you @scūriolus ! I knew mine stopped working out of nowhere around that time, but didn't know why – Brendan Goggin Apr 06 '23 at 22:48
  • 1
    `python.analysis.autoImportUserSymbols` does no longer exist (you will have an info message saying: Unknown Configuration Setting) – YellowStrawHatter Aug 24 '23 at 15:06
  • Thank you @YellowStrawHatter, updated to reflect that – Brendan Goggin Aug 28 '23 at 19:32
8

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.

Carl
  • 853
  • 9
  • 23
Eric
  • 741
  • 6
  • 10
6

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.

Eyal Levin
  • 16,271
  • 6
  • 66
  • 56
3

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,

James
  • 325
  • 1
  • 3
  • 15
  • 2
    Autoimport has been disabled by default in the November 2022 release of VS Code because of how "*annoying it can be to have an import automatically added to a file*" (that was a [Github request](https://github.com/microsoft/pylance-release/issues/2344)). Source : https://devblogs.microsoft.com/python/python-in-visual-studio-code-november-2022-release/#auto-imports-turned-off-by-default-with-pylance – scūriolus Apr 03 '23 at 21:18
2

I use this package it works very well

https://marketplace.visualstudio.com/items?itemName=codeavecjonathan.importmagic

Mahdi mehrabi
  • 1,486
  • 2
  • 18
  • 21
1

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.

bhargav3vedi
  • 521
  • 1
  • 6
  • 11
  • I use `webargs` in my project and it didn't suggest importing `fields` from it (the code is `fields.Integer(<...>)`, that's disappointing. Although if I use `webargs.fields.Integer(<...>)` it works. – Oleg Yablokov Feb 16 '22 at 15:06
1

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:

enter image description here

Then, pressing Enter can automatically import what you select as shown below:

enter image description here

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:

enter image description here

Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129
0

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.

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jun 21 '22 at 02:04
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/late-answers/32066766) – BrokenBenchmark Jun 25 '22 at 14:37