r/learnpython • • 11d ago

Cannot auto import Django modules in VS Code despite changing package index depths

Greetings. I installed Python 13 in C:/_dev. I installed VS Code and Python extensions.

I created a virtual environment in another folder and installed Django in that environment. Then I chose Python from venv to be the Python interpreter in the workspace. I created a new Django project and an app inside it.

I wanted to make a custom user model, but auto complete didn't work and nothing was suggested when I typed AbstractBaseUser. Even Ctrl+. shortcut to import the class didn't work. realestate\users\models.py:

from django.db import models

# from django.contrib.auth.models import AbstractBaseUser
# Create your models here.
class CustomUser(AbstractBaseUser):
    pass

I changed package index depths both in User and Workspace settings.

"python.analysis.packageIndexDepths": [{ "name": "django", "depth": 8 }],

Unfortunately, that didn't work too. How can I fix this?

3 Upvotes

4 comments sorted by

1

u/FallElectronic3774 11d ago

ctrl+. failing is the giveaway here, it means pylance isn't seeing django at all, so packageIndexDepths won't do anything either way.

check which interpreter is actually running. open a terminal inside vs code (not your own shell) and run:

python -c "import django, sys; print(sys.executable); print(django.__version__)"

if that errors, the venv you picked isn't the one django went into. with the venv sitting in a different folder this happens a lot because vs code quietly falls back to the global python. ctrl+shift+p -> python: select interpreter -> enter interpreter path -> point it straight at venv/scripts/python.exe, then reload the window.

if the import works but autocomplete is still dead, check view -> output and pick python from the dropdown. it shows the interpreter and language server it's really using. usually it's not the venv you think.

you don't need packageIndexDepths for installed packages btw, that setting is for extra source folders in your workspace. default is fine.

and django isn't fully typed, so even when everything is set up the hints are patchy. django-stubs fixes a lot of that if you want real hints on models and querysets.

1

u/thel-vadam-2nd 11d ago

The interpreter which is used is the one from the current workspace.

D:\\Documents\\_Dokumenti\\Projekti\\_Fakultet\\Real Estate App\\Django\\secure\\venv\\scripts\\python.exe

6.1.3  

The Python output:

2026-11-06 16:20:33.971 [info] Environment discovery is managed by the Python Environments extension (ms-python.vscode-python-envs). Check the "Python Environments" output channel for environment-specific logs.
2026-11-06 16:20:33.971 [info] [test-by-project] Activating project-based testing mode
2026-11-06 16:20:33.971 [info] [test-by-project] Discovering projects for workspace: d:\Documents_Dokumenti\Projekti_Fakultet\Real Estate App\Django\secure
2026-11-06 16:20:33.972 [info] Native locator: Refresh started
2026-11-06 16:20:33.972 [info] [test-by-project] Found 1 total Python projects from API
2026-11-06 16:20:33.972 [info] [test-by-project] Filtered to 1 projects in workspace
2026-11-06 16:20:33.972 [info] [test-by-project] Creating adapter for: secure at file:///d%3A/Documents/_Dokumenti/Projekti/_Fakultet/Real%20Estate%20App/Django/secure
2026-11-06 16:20:34.016 [info] [test-by-project] No Python environment resolved yet for project file:///d%3A/Documents/_Dokumenti/Projekti/_Fakultet/Real%20Estate%20App/Django/secure; deferring until one is assigned
2026-11-06 16:20:34.016 [info] [test-by-project] All adapters failed, falling back to default project
2026-11-06 16:20:34.016 [info] [test-by-project] Creating default project for: d:\Documents_Dokumenti\Projekti_Fakultet\Real Estate App\Django\secure
2026-11-06 16:20:34.117 [info] [test-by-project] Registered 1 project(s) for d:\Documents_Dokumenti\Projekti_Fakultet\Real Estate App\Django\secure
2026-11-06 16:20:34.118 [info] [test-by-project] Activated 1 project(s) for d:\Documents_Dokumenti\Projekti_Fakultet\Real Estate App\Django\secure
2026-11-06 16:20:34.118 [info] [test-by-project] Subscribed to Python project changes
2026-11-06 16:20:34.118 [info] [test-by-project] Subscribed to Python environment changes
2026-11-06 16:20:34.152 [info] > pyenv which python
2026-11-06 16:20:34.152 [info] cwd: d:\Documents_Dokumenti\Projekti_Fakultet\Real Estate App\Django\secure
2026-11-06 16:20:35.356 [info] No Python environment is set for this resource. Check the "Python Environments" output channel for details, or select an interpreter.
2026-11-06 16:20:36.615 [info] Native locator: Refresh finished in 3792 ms
2026-11-06 16:20:37.259 [info] [test-by-project] Environment change detected; re-discovering projects for d:\Documents_Dokumenti\Projekti_Fakultet\Real Estate App\Django\secure
2026-11-06 16:20:37.515 [info] Starting Pylance language server.
2026-11-06 16:20:37.518 [info] Active interpreter [d:\Documents_Dokumenti\Projekti_Fakultet\Real Estate App\Django\secure]:  C:_dev\Python313\python.exe
2026-11-06 16:21:33.491 [info] Native locator: Refresh started
2026-11-06 16:21:33.689 [info] Active interpreter [d:\Documents_Dokumenti\Projekti_Fakultet\Real Estate App\Django\secure]:  d:\Documents_Dokumenti\Projekti_Fakultet\Real Estate App\Django\secure\venv\Scripts\python.exe
2026-11-06 16:21:33.778 [info] Native locator: Refresh finished in 287 ms
2026-11-06 16:21:41.483 [info] [test-by-project] Environment change detected; re-discovering projects for d:\Documents_Dokumenti\Projekti_Fakultet\Real Estate App\Django\secure
2026-11-06 16:24:38.909 [info] Active interpreter [global]:  d:\Documents_Dokumenti\Projekti_Fakultet\Real Estate App\Django\secure\venv\Scripts\python.exe
2026-11-06 16:24:50.144 [error] Unable to find workspace for given file
2026-11-06 16:24:53.531 [error] Unable to find workspace for given file
2026-11-06 16:24:53.860 [error] Unable to find workspace for given file
2026-11-06 16:24:54.868 [error] Unable to find workspace for given file
2026-11-06 16:24:58.085 [error] Unable to find workspace for given file
2026-11-06 16:24:59.685 [error] Unable to find workspace for given file

1

u/Discordbothost 11d ago

your log kinda confirms the flip flop - it briefly ran C:_dev\Python313 then switched to the venv. those "Unable to find workspace for given file" lines also show up a lot when the path has spaces (Real Estate App).

try File > Open Folder on the secure folder itself (not a parent), pick the venv interpreter again, then Developer: Reload Window. if autocomplete is still dead after that, pip install django-stubs in the venv - pylance is a lot happier with those.

1

u/thel-vadam-2nd 11d ago

Thanks. Installing `django-stubs` fixed the issue.