Cannot auto import Django classes in VS Code despite changing package index depth
I installed Python 3.13 in C:/_dev/ folder.
I have created a Python virtual environment in another folder and installed Django, then I generated a Django project and an application.
I wanted to make a custom User model. When I typed class CustomUser(AbstractBaseUser), AbstractBaseUser was not suggested and imported. When I pressed Ctrl+. on AbstractBaseUser nothing was suggested too.
from django.db import models
# Create your models here.
class CustomUser(AbstractBaseUser): // from django.contrib.auth.models import AbstractBaseUser
pass
I changed package index depths both in workspace and user settings.
"python.analysis.packageIndexDepths": [{ "name": "django", "depth": 5 }]
Nothing was still suggested.
How do I solve the problem? I also tried restarting VS Code, but that didn't work.
Pylance already indexes Django 4 levels deep by default. what's likely missing is includeAllSymbols. Without it, packages that have no py.typed only contribute the names listed in each module's __all__. Django has neither, so AbstractBaseUser never makes it into the index, which is also why Ctrl+. finds nothing
"python.analysis.packageIndexDepths": [
{ "name": "django", "depth": 4, "includeAllSymbols": true }
],
"python.analysis.autoImportCompletions": true
The second line is for the suggestions while you type, which Pylance turns off by default.
Read more here
https://github.com/microsoft/pylance-release/blob/main/README.md
https://github.com/microsoft/python-type-stubs
https://devblogs.microsoft.com/python/python-in-visual-studio-code-november-2022-release/
Try adding "includeAllSymbols": true to your python.analysis.packageIndexDepths setting:
"python.analysis.packageIndexDepths": [
{
"name": "django",
"depth": 4,
"includeAllSymbols": true
}
],
"python.analysis.autoImportCompletions": true
Then restart the Python Language Server using Python: Restart Language Server from the Command Palette.
This should enable Pylance to index Django symbols that are not explicitly exported.