Python auto-import extension for VSCode
Asked Answered
S

10

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.

Shoulder answered 14/10, 2017 at 9:39 Comment(0)
W
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
Wiggler answered 1/7, 2020 at 9:51 Comment(7)
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". :(Kristiekristien
So how do you do auto imports with Pylance? I couldn't find itKuebbing
Pylance is proprietary and not open source, another microsoft foot-in-the-door scam. So do not try to advertise that. DOWNVOTING this.Quid
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.Enswathe
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"Levitation
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)Frentz
doesn't work: (Pylance in the settings: python.analysis.autoImportCompletions is checked in settings) but "Exception has occurred: ModuleNotFoundError No module named 'plyer". Bard says that it's a known bug (pylance doesn't always works for this)Junitajunius
G
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.

Greybeard answered 22/1, 2023 at 18:21 Comment(2)
autoImportUserSymbols did it for me - thank you alot! In my case it was also required to add: "python.autoComplete.extraPaths": ["${workspaceFolder}/scripts"]Coopery
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/…Clang
M
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).

Mcintire answered 3/3, 2018 at 11:9 Comment(2)
Postponed: github.com/Microsoft/vscode-python/pull/…Hanyang
Not working for me, but with Pylance everything does. I simply ctrl+dot / cmd+dot on a new class and it allows me to add a new import.Thorson
S
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.

Shampoo answered 23/9, 2018 at 16:29 Comment(2)
Extension broken as of 2021/08Remitter
This extension has gone stale - no commits since September 2020.Iggie
W
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.

Wiggler answered 3/3, 2020 at 7:30 Comment(0)
A
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,

Adenine answered 14/1, 2023 at 5:55 Comment(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). Source : devblogs.microsoft.com/python/…Clang
S
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

Saphena answered 13/6, 2023 at 10:52 Comment(0)
M
2

I use this package it works very well

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

Malathion answered 31/8, 2020 at 1:16 Comment(0)
W
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.

Wolter answered 20/5, 2021 at 16:21 Comment(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.Kuebbing
C
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...

Courlan answered 14/11, 2023 at 22:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.