Fix the 'Unable to import django.db' error in VS Code
By Flavio Copes
Fix the 'Unable to import django.db' pylint error in VS Code by running Python: Select Interpreter and choosing your project's virtual environment.
The Unable to import 'django.db' pylint(import-error) error in VS Code means the editor is using the wrong Python interpreter. Point it at your project’s virtual environment and the error goes away.
While working on a Django app in VS Code you might run into this. VS Code underlines the from keyword in an import, and if you hover it with the mouse you will see the error Unable to import 'django.db' pylint(import-error) showing up.
Why does this happen?
When you create a Django project, you install Django inside a virtual environment. That’s a self-contained folder holding a Python interpreter and the packages of that one project, isolated from the rest of the system.
VS Code runs pylint using the interpreter selected for the workspace. If that’s the system Python instead of your virtual environment, pylint looks for Django there, doesn’t find it, and flags every Django import as an error.
The code is fine. Your app runs. Only the linter is looking in the wrong place.
The fix
To fix it, run cmd-shift-p (or click View -> Command Palette) and run the command Python: Select Interpreter.

VS Code will show you a list of Python interpreters found.

Choose the one corresponding to your virtual environment, in this case the last option.
The error will disappear.
VS Code remembers the choice for the workspace, so you only do this once per project.
What if the error is still there?
Sometimes the squiggles stick around after switching. Run Developer: Reload Window from the Command Palette to restart the linter with the new interpreter.
If it still complains, check that Django is actually installed in that environment. Activate it in the terminal and run:
pip show django
No output means Django is missing from the environment you selected. Install it there with pip install django and the import resolves.
The same problem shows up with any package, not just Django. If pylint says Unable to import 'requests' or Unable to import 'rest_framework' while your app runs fine, it’s the same wrong-interpreter issue, with the same fix.