[Django]-VS Code error when importing Django module

84๐Ÿ‘

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) environment
No 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
๐Ÿ‘คShashank Rawat

14๐Ÿ‘

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.

๐Ÿ‘คOptider

9๐Ÿ‘

  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

๐Ÿ‘คto-serve

5๐Ÿ‘

  1. Ctrl+Shift+P

  2. Type Python:select interpreter
    Now we will get:

    enter image description here

  3. Choose Enter interpreter path:
    enter image description here

  4. Select Find...
    Then
    enter image description here

๐Ÿ‘คmaguluri

3๐Ÿ‘

Check correct django version is properly installed and active?

In the active environment, calling this code in python interpreter shouldnโ€™t have errors.

from django.conf.urls import url

Check the VS studio python environment
https://code.visualstudio.com/docs/python/environments

๐Ÿ‘คKyle Goetz

3๐Ÿ‘

You need to select the right environment. So, go to view in tool bar, then select command pallet(ctrl+shift+p), then type "python:select interpreter", then select the right virtual environment where you start you project.

1๐Ÿ‘

In my case I solved it using the Select Interpreter option from VS Codeโ€™s Command Palette (Shift + Command + P).

I chose the Python interpreter option which corresponds with the folder in which my virtual environment was and it solved the issue immediately.

Hope it helps ๐Ÿ™‚

๐Ÿ‘คuser10048802

0๐Ÿ‘

Are you using a virtual environment (mkvirtualenv)? In that case you need to make sure you install django and pylint etc., within your virtual environment too, using the following commands.

  1. workon [yourEnvName]

  2. pip install pylint

  3. pip install django

  4. pip install djangorestframework

and so onโ€ฆfor all the modules you want to use.

๐Ÿ‘คSubbu

0๐Ÿ‘

enter image description here

Choosing a global environment helped me to recover this issue

๐Ÿ‘คVenkat

0๐Ÿ‘

First check the requirements that you install djangorestframework, second if you work on virtual envs maybe the pylint that you use not check in your virtual env so you can install in local the package.

0๐Ÿ‘

For me, I opened the nested folder of my Django project. So, Django couldnโ€™t find the modules of venv.

Just mentioning, I thought it might be helpful to someone.

๐Ÿ‘คMejan

Leave a comment