How to resolve 'Import "django.contrib" could not be resolved from source' in VS Code?

This error suddenly came up when I created a new Django project. I used pip to install all the packages in the virtual environment.


I had exactly the same problem and there are several solutions out there, but only @Neha's answer has brought me to the point. The problem here is really simple. VS Code uses your "main" Python interpreter, whereas you should use the one in your virtual environment for Django.

In other words, I have my main Python installation here: c:\users\yourName\appdata\local\programs\python\python39.

And my VS Code has pointed to exactly this interpreter. But my Django was installed in this path: c:\users\yourName\source\vscode-repos\basic-app\backend-service\venvs\lib\site-packages.

In order to solve it click on the VS Code bar with Python Interpreter, you will then see a pop-up window. Choose + Enter interpreter path... then Find.... It will open File Explorer for you. Go to your virtual environment folder, where you have your Django installed (in my case it the vscode-repos\basic-app\backend-service\venvs folder), inside of it go to the Scripts folder and pick python.exe, like this. And you are good to go!


Select the python interpreter in which u have created the virtual environment. I got the same error and this solution worked. Attached a screenshot u can refer to that. https://i.stack.imgur.com/pCbuB.jpg