VS Code error when importing Django module VS Code error when importing Django module django django

VS Code error when importing Django module


I too was facing this error while working with Python virtual environments. In my case, it was happening because I have installed Django on my virtual environment and my base environment didn't contain any module named Django.

Base(Global) environmentNo module named Django in my global enviroment

and when I use the same command inside my virtual environment (myDjangoEnv)enter image description here

Fix:

  1. Now what I understood is that pylint uses the python interpreter to analyze our code and mark the error while we write the code.
  2. Since I have three python environments in my system, hence three different python interpreters are available to my VS Code.

So if VS code uses the Python interpreter from the base environment, it will not recognize the Django module (import Error). Therefore, you are required to change the Python interpreter to the one present in your virtual environment.

It sounds complicated but it is pretty simple:

  1. Click on the bottom left of the screen to change python interpreter. changing python interpreter
  2. Select from the list of available Python interpreters. Make sure you select the appropriate interpreter with respect to the current project. enter image description here


  1. Locate your project's virtual environment. In my case, I am working on a Django project and my virtual environment is located on the path below:

    C:/Users/abc/Desktop/Virutal36/myLab/Scripts/python.exe  
  2. Copy the address of your virtual environment.

  3. On VS Code, Select File > Preferences> Settings to open your User Settings (or use the Ctrl+, shortcut).

  4. Create or modify an entry for python.pythonPath with the full path to your virtual environment and you will be good to go. In my case it is:

    C:/Users/abc/Desktop/Virutal36/myLab01/Scripts/python.exe

https://code.visualstudio.com/docs/python/environments#_manually-specify-an-interpreter


Follow steps mentioned in the image. For details, or if that doesn't work, read further!!!

enter image description here

If you can't see your interpreter (installed in the virtual environment) listed in the drop-down list OR selecting interpreters listed don't rectify the error.

Then, you have to give the path of your interpreter (installed in venv) to vs code. Because you might have installed Django only in your venv. Happens when you don't use anaconda to create venv.

Steps to rectify-

  1. To check the path, activate venv and type which python in terminal, this will give path. Copy the path.

  2. Click interpreter on lower left, to pull drop-down, as shown in pic above.

  3. Click enter the interpreter path.

  4. Paste path copied.

This will assign the right interpreter and rectify the error.