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

Answer from Brendan Goggin on Stack Overflow
🌐
Visual Studio Marketplace
marketplace.visualstudio.com › items
Python Auto Import - Visual Studio Marketplace
Extension for Visual Studio Code - Auto-import suggestions for modules already imported.
Discussions

VSCode: Auto import/import suggestions for Python dependencies from external libraries
I'm using VSCode for Python programming. One project heavily relies on an external library and often needs to load modules/classes/etc from that library. When I type a class that is not yet importe... More on github.com
🌐 github.com
2
July 15, 2022
Is there any Auto Import Python extension available or in is development for VS Code
I think there is many auto import extension, but not for Python, which is very extensive for work with VS Code. I am sorry if this feature is already suggested, but like to contribute if it is in d... More on github.com
🌐 github.com
2
March 16, 2019
VS Code Python auto Import - Stack Overflow
I am missing the auto import feature in VS Code for Python development. I have the official Python plugin installed which should have solved this. My python directory has __init__.py and when I ope... More on stackoverflow.com
🌐 stackoverflow.com
Recently Active 'auto-import' Questions - Stack Overflow
In VSCode, how can I get auto import suggestions for React built-in hooks, like useState, useEffect, etc? Just like what we get in CodeSandbox: Enabled extensions (maybe there is some conflict ... ... Is there a Python auto-import extension/plugin available for VSCode? More on stackoverflow.com
🌐 stackoverflow.com
People also ask

Does Pylance support older versions of Python?
A: Pylance supports Python 3.6 and above. Make sure your projects are compatible with these versions to take full advantage of the features.
🌐
sqlpey.com
sqlpey.com › python › python-auto-imports-vscode
How to Enable Python Auto-Imports in VSCode: A Comprehensive Guide
Can I use Pylance with other languages?
A: Pylance is specifically designed for Python, but VSCode supports various language servers that can provide similar features for other languages.
🌐
sqlpey.com
sqlpey.com › python › python-auto-imports-vscode
How to Enable Python Auto-Imports in VSCode: A Comprehensive Guide
🌐
Reddit
reddit.com › r/vscode › why is the python auto import feature still such garbage compared to pycharm
r/vscode on Reddit: Why is the Python Auto Import feature still such garbage compared to PyCharm
October 19, 2021 -

I've switched from PyCharm to VSCode a few months ago, mostly because VSCode is faster and I use it anyhow for JS development.

For me, one of the most important features of an IDE/Editor is quickly add imports, and I just don't understand how VSCode has such a great reputation but is so so bad at this for Python. I half suspect/hope that this due to some misconfiguration from me, so please help me out if this is working good for you!

Here are some examples that I keep running into:

#1: Import Typings

Let's say I'm defining a function:

def foo(bar: List[Dict[str, Any]]):
    pass

And I wanna import the type definitions. Every reasonable IDE would put the from typing import ... suggestion at the very top. VSCode gives me 20+ partially matched suggestions that are alphabetically sorted sorted and I have to find the correct one. In contrast: PyCharm puts it on the top and it takes me a few milliseconds to import it. VSCode on the other hand rips me out of my flow and I have to search for 10+ seconds.

#2: Import from Project Files

This just happened to me: I have a Python project and created a new file shared.py where I defined an object with a pretty unique name. If I go to another file and write it, VSCode is completely lost. It has no clue what that is and where it could import it from. In contrast: PyCharm mostly get's it right, but takes a second or two sometimes.

#3: Auto-Import messes up the Code

In the cases VSCode actually finds suggestions from my code, it often keeps messing it up by confusing files with variables.

Example:

I have a file myproject/settings.py that contains a Pydantic class called Settings(BaseSettings):... and a variable settings = Settings(). Now, I see that these three kinds of "settings" can be confusing: The file, the class and the variable. However, PyCharm has a smart enough heuristic to understand that most of the time, I want to import the variable settings and puts it as the first suggestion. VSCode, on the other hand, doesn't even get that: The only suggestions I get are the file import settings OR the class from settings import Settings. You have no idea how often I quickly picked the second one, only to realise that VSCode not only added the wrong import, but also changed my code from settings to Settings, messing up my code.

Question: What to do?

My goal isn't to bash VSCode, because I want to keep using it. I just notice more and more how it gets in the way and keeps disrupting my flow with bad suggestions and wrong imports. And I find it hard to believe I'm the only one who's suffering from this, so please share your experience! It would be a shame if I have to move back to PyCharm and say goodbye to countless GB of RAM...

Here is my general setup / things I tried:

  • Python interpreter is correctly configured and VSCode restarted after

  • Interpreter is coming from a Poetry virtual env

  • Extensions:

    • Python

    • Pylance

    • Visual Studio IntelliCode

  • Relevant Settings:

    "python.diagnostics.sourceMapsEnabled": true,
    "vsintellicode.features.python.deepLearning": "enabled",
    "python.analysis.indexing": true,  // Found that somewhere on SO
    "python.languageServer": "Pylance",
    "python.autoComplete.addBrackets": true,
    "python.analysis.completeFunctionParens": true

I would really love to hear what you folks have to say to that and how I can improve my python experience in VSCode!

🌐
Youssefm
youssefm.com › posts › writing-python-in-vscode
Writing Python in VS Code just got a whole lot better
Developing Python applications in VS Code just got a lot better this week with the release of Pylance version 2022.12.21. It introduces a new option that lets you auto import user symbols, saving you time and reducing interruptions to your flow.
🌐
Visual Studio Code
code.visualstudio.com › docs › python › editing
Editing Python in Visual Studio Code
November 3, 2021 - As you type in the editor, you might get completion suggestions. When you accept the suggestion, auto import automatically adds the corresponding import statement to your file.
Find elsewhere
🌐
GitHub
github.com › microsoft › pylance-release › issues › 3055
VSCode: Auto import/import suggestions for Python dependencies from external libraries · Issue #3055 · microsoft/pylance-release
July 15, 2022 - I'm using VSCode for Python programming. One project heavily relies on an external library and often needs to load modules/classes/etc from that library. When I type a class that is not yet imported and click on "quick fix" (cmd + . on Mac), VSCode automatically suggests what to import.
Author   microsoft
🌐
sqlpey
sqlpey.com › python › python-auto-imports-vscode
How to Enable Python Auto-Imports in VSCode: A Comprehensive Guide
November 24, 2024 - Signature Help: Get type information along with parameter suggestions as you code. Code Completion: Experience intelligent auto-completion for Python syntax and functions. Auto-import Functionality: Automatically import modules when you reference them in your code.
🌐
Microsoft Developer Blogs
devblogs.microsoft.com › dev blogs › microsoft for python developers blog › python in visual studio code – november 2022 release
Python in Visual Studio Code - November 2022 Release - Microsoft for Python Developers Blog
November 3, 2022 - The November 2022 release of the Python and Jupyter extensions for Visual Studio Code are now available. This release includes automatic installation of the isort extension, auto imports turned off by default with Pylance, "just my code" cell debugging in Jupyter and more!
🌐
GitHub
github.com › microsoft › vscode-python › issues › 4780
Is there any Auto Import Python extension available or in is development for VS Code · Issue #4780 · microsoft/vscode-python
March 16, 2019 - I think there is many auto import extension, but not for Python, which is very extensive for work with VS Code. I am sorry if this feature is already suggested, but like to contribute if it is in d...
Author   microsoft
🌐
Microsoft Developer Blogs
devblogs.microsoft.com › dev blogs › microsoft for python developers blog › python in visual studio code – november 2019 release
Python in Visual Studio Code – November 2019 Release - Microsoft for Python Developers Blog
June 3, 2020 - Automatic imports functionality was one of the most requested features on our GitHub repo (GH21), and when you enable the Microsoft Language Server, you will get this new functionality. To enable the Language Server, add the setting “python.jediEnabled”: false to your settings.json file.
🌐
Microsoft Developer Blogs
devblogs.microsoft.com › dev blogs › microsoft for python developers blog › the pylance language server has reached stable!
The Pylance language server has reached stable! - Microsoft for Python Developers Blog
May 13, 2021 - Earlier this week, we announced that as of the May release of the Python extension, Pylance is also now the default Python language server in Visual Studio Code. Pylance is also now included in the Python core extension’s bundle as an optional dependency, meaning that we’ll automatically install it for you, but you have the option to use another language server of your choice.
🌐
Stack Overflow
stackoverflow.com › questions › tagged › auto-import
Recently Active 'auto-import' Questions - Stack Overflow
Goal VScode autoimports my imports as absolute path except the file is in same directory. Example Assuming we have ComponentA and ComponentB in the same directory, but a service in another directory. ... ... I use emacs with python-lsp-server (pylsp) for python development (I attached my setup below).
🌐
GitHub
github.com › microsoft › vscode-python › issues › 21
Implement auto imports · Issue #21 · microsoft/vscode-python
November 9, 2017 - One of the few missing features is to have auto imports. Something like what was just released in vscode 1.18 for javascript would be great.
Author   microsoft
🌐
k0nze
k0nze.dev › posts › python-relative-imports-vscode
Python Relative Imports in VSCode (Fix ModuleNotFoundError and Auto-completion) | k0nze
January 30, 2024 - Every file that imports MyClass has to be changed, which is tedious and error-prone. A better solution would be to have a single file to extend the PYTHONPATH. You can either extend PYTHONPATH systemwide by appending the full path to libs to it, whereas several paths are separated using a colon :. But then you have to tell everyone who uses your code to do this for their system. So the preferred solution is to ask VSCode to extend the PYTHONPATH only for your project which you can also add to your git repository such that others don’t have to extend their PYTHONPATH manually.
🌐
Toolify
toolify.ai › gpts › troubleshooting-python-imports-and-autocompletion-in-vscode-110831
Troubleshooting Python Imports and Auto-completion in VSCode
To enable the autocompletion of relative imports in Visual Studio Code, we need to Create a launch.json file. This file allows us to configure the Python environment and set up the necessary paths for our project.