Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VS Code error when importing Django module

Tags:

I'm working on a web app and I use Django as framework.
I'm using VS Code on a macOS.

I get an error when I try to import some Django module. This is a screenshot of my code in error.

Image of my code in error

The error message is the following:

[pylint] E0401:Unable to import 'django.conf.urls'

like image 906
K. BLEU Avatar asked Jan 15 '18 20:01

K. BLEU


People also ask

How do I fix the import error in VS code?

To solve unresolved import error in Python, set your Python path in your workspace settings. If you are working with Visual Studio Code and import any library, you will face this error: “unresolved import”. Then reload the VSCode, and it will fix that error.

Why is VSCode not recognizing Python import?

The message simply means that VSCode cannot detect the correct path for a Python module. The cause of "Unresolved Import" could be one of the following reason: VSCode is using the wrong Python path. This is often the case if you're running your code against a virtual environment.

What is import error in Django?

db' pylint(import-error) showing up. This is because VS Code is not running the Virtual Environment of the app. To fix it, run cmd-shift-p (or click View -> Command Palette and run the command Python: Select Interpreter. VS Code will show you a list of Python interpreters found.


2 Answers

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
like image 192
Shashank Rawat Avatar answered Oct 06 '22 04:10

Shashank Rawat


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.

like image 34
Optider Avatar answered Oct 06 '22 02:10

Optider