92

Is there a Python auto-import extension/plugin available for VSCode?

By auto-import I mean automatically importing of Python modules, so if you type sys.argv then it should automatically import the sys module. Eclipse and IntelliJ have this feature with Java.

0

10 Answers 10

36

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
7
  • 21
    Note that Pylance is closed-source and that the Microsoft Python Language Server will be deprecated in favor of Pylance. Looks like Microsoft is back to playing the good ol' game of "embrace extend extinguish". :(
    – balu
    Commented Apr 26, 2021 at 15:23
  • 4
    So how do you do auto imports with Pylance? I couldn't find it Commented Feb 16, 2022 at 14:35
  • 7
    Pylance is proprietary and not open source, another microsoft foot-in-the-door scam. So do not try to advertise that. DOWNVOTING this. Commented Apr 4, 2022 at 4: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
    Commented May 20, 2022 at 12:34
  • 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) Commented Nov 19, 2022 at 13:54
32

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

2
  • 1
    autoImportUserSymbols did it for me - thank you alot! In my case it was also required to add: "python.autoComplete.extraPaths": ["${workspaceFolder}/scripts"] Commented Feb 21, 2023 at 8: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). On my side, I've turned it on and I rely on my linter (ruff) to check for unused imports. Source : devblogs.microsoft.com/python/…
    – scūriolus
    Commented Apr 3, 2023 at 21:17
13

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

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

2
  • 5
    Extension broken as of 2021/08
    – sorin
    Commented Aug 14, 2021 at 8:44
  • This extension has gone stale - no commits since September 2020.
    – Sebastian
    Commented Jan 27, 2023 at 14:44
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.

4

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,

1
  • 3
    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). Source : devblogs.microsoft.com/python/…
    – scūriolus
    Commented Apr 3, 2023 at 21:18
4

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

2

I use this package it works very well

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

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.

1
  • 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. Commented Feb 16, 2022 at 15:06
0

{Nov. 2023}

In my case, pylance installed as extension. Then press (ctrl+p), type > and find "Extensions: Enable auto update for all extensions".

enter image description here

After that click on "{}Python > Auto Import Completition as true" in down right corner.

other options in the settings.json not worked...

Not the answer you're looking for? Browse other questions tagged or ask your own question.