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

55👍

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!

13👍

Select the Python interpreter in which you have created the virtual environment. I had the same error and this solution worked.

Below is a screenshot that you can refer to.

👤Neha

8👍

Actually all of the answers were right , but what worked for me is:

  1. Type pip show Django in vsc terminal
  2. Go to the path of intallation mentioned there
  3. It will be inside "lib" by default..go back to scripts
  4. Inside the scripts , there will be python .exe app
  5. Choose this as your interpreter (as per above answers)

4👍

If you are using VScode, click the python version on the bottom left corner and select the venv where you have installed the packages.

1👍

For anyone who can’t see the virtual environment then click on the python version below right corner of vscode,click on Enter Interpretor Path,click find,chose the folder that you are working,click on virtual environment name ,then click bin and select python version and the warning will be gone.

👤Rafi

1👍

In my case (on MacOS) the problem was caused by creating venv using default arguments.

I don’t have Scripts directory, but I’ve got bin inside. The python3 -m venv my_venv command was creating symlinks to python and python3 executables which was confusing for the VSCode. After removing and creating again the venv with below command everything started to work

python3 -m venv --upgrade-deps --copies my_venv

0👍

Go to the Python(Extentions) below your screen Like this then select interpreter tab will pop up, then select C:\Python39\python.exe Like this.Hope it helps

0👍

What worked for me was to install django within my virtualenv. All other settings were correct in Visual Studio Code.

👤Rock

0👍

I had also face same problem but simple solution is configure your main python interpreter path from this option +Enter interpreter path..

which common path can be like this either may be different :

C:\Users\YOURNAME\AppData\Local\Programs\Python\Python311\python.exe

After selecting that path, Here your error get solved and it will removed orange wavy underlines.

  • For windows OS

0👍

You could as well add:

# pylint: disable=import-error

to the top of your file.

0👍

If you are using VScode, run:
cnrl+shift+p
then select Python: Select Interpreter
finally, select your current virtual environment directory from your project.
I hope it should be fixed.

0👍

If you are using MacBook, follow the below steps:

Step 1: Open the search console by hitting command+shift+p

Step 2: Search for – Python: Select Interpreter (Refer to screenshot)
enter image description here

Step 3: Select the venv path from the dropdown
enter image description here

This should fix the error you are seeing.

Leave a comment