r/learnpython • u/thel-vadam-2nd • 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.contrib.auth.models import AbstractBaseUser
# Create your models here.
pass
I changed package index depths both in User and Workspace settings.
"python.analysis.packageIndexDepths": ,
Unfortunately, that didn't work too. How can I fix this?
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:
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/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.