(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.
(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.
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
VSCode: Auto import/import suggestions for Python dependencies from external libraries
Is there any Auto Import Python extension available or in is development for VS Code
VS Code Python auto Import - Stack Overflow
Recently Active 'auto-import' Questions - Stack Overflow
Does Pylance support older versions of Python?
Can I use Pylance with other languages?
Videos
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": trueI would really love to hear what you folks have to say to that and how I can improve my python experience in VSCode!
Auto imports are not supporting by the Python extension at the moment (you may have turned it on for another language like TypeScript). You can upvote the issue tracking this feature request to help show that you would like to see it implemented.
Pylance implemented a code action for adding imports for unknown symbols circa Oct 2019. See https://github.com/microsoft/python-language-server/pull/1656. Not sure what the current status is / whether it has changed in any way since then.
Also related: https://github.com/microsoft/python-language-server/issues/19 and https://github.com/microsoft/vscode-python/issues/21.
What finally worked for me is this setting:
"python.analysis.extraPaths": [
"~/Projects/mylib"
],
"python.analysis.autoImportCompletions": false,
"python.analysis.packageIndexDepths": [
{
"name": "mylib",
"depth": 5,
"includeAllSymbols": false
}
],
"python.autoComplete.extraPaths": [
"~/Projects/mylib"
],
With this, I can add missing imports simply with Cmd + . (on MacOS).
If setting autoImportCompletions to true, it would even add the imports automatically.
Thanks MingJie for the hint regarding the index depth. This was certainly a key issue.
Not sure if the relative paths and the analysis.extraPaths was really needed, but now everything works.
Usually, vscode only performs first-class retrieval. We can add the following content to make vscode perform deep retrieval:
"python.analysis.packageIndexDepths":[["sqlalchemy",2]],
In this example, sqlalchemy.orm is 2 level deep.
Of course, the load on hardware will also be greater.
You can also use [["",2]], it will change default depth.
You can refer to this issue in github for more information.
How to make possible auto imports from Django Rest Framework in VSCode.
I have Pylance extension but it doesn't work