Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 754
Mrinmoy Sarkar Avatar asked Dec 01 '25 03:12

Mrinmoy Sarkar


1 Answers

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!

like image 98
Begli Amanov Avatar answered Dec 03 '25 07:12

Begli Amanov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!