How to fix the error “Unable to import django.db” in Django app using VS Code
When working on a Django app using VS Code, you may encounter an error where the from
keyword in an import is underlined and hovering over it displays the error message “Unable to import ‘django.db’ pylint(import-error)”.
This error occurs because VS Code is not running the virtual environment of the app. Fortunately, there is a simple solution to fix this issue:
Open VS Code and navigate to the file where the error is occurring.
Press
cmd-shift-p
(or clickView -> Command Palette
) to open the command palette.In the command palette, search for and select the command Python: Select Interpreter.
VS Code will display a list of Python interpreters that it has found.
Choose the interpreter that corresponds to your virtual environment. It is usually listed as the last option in the list.
Once you have selected the correct interpreter, VS Code will automatically switch to using that interpreter.
By following these steps, you should be able to resolve the “Unable to import django.db” error in your Django app in VS Code. Now you can continue working on your app without any import errors.
tags: [“Django”, “VS Code”, “ImportError”, “Python interpreter”, “virtual environment”]