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:
And my VS Code has pointed to exactly this interpreter. But my Django was installed in this path:
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!
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.
- Django: How do I use a string as the keyword in a Q() statement?
- Client side JS (e.g. AngularJS) + Django REST Backend: Deploy onto single PaaS?
- Enabling Django Admin Filters on Many-to-Many Fields
Actually all of the answers were right , but what worked for me is:
pip show Djangoin vsc terminal
- Go to the path of intallation mentioned there
- It will be inside "lib" by default..go back to scripts
- Inside the scripts , there will be python .exe app
- Choose this as your interpreter (as per above answers)
- Python Django requirements.txt
- Using python string formatting in a django template
- Restricting all the views to authenticated users in Django
- Django newbie deployment question – ImportError: Could not import settings 'settings'
- Django & the "TemplateDoesNotExist" error
If you are using VScode, click the python version on the bottom left corner and select the venv where you have installed the packages.
- Django Get Absolute URL
- 404 because of restarting the webpack-dev-server
- RuntimeError: __class__ not set defining 'AbstractBaseUser' as <class 'django.contrib.auth.base_user.Abstract BaseUser'>. Was __classcell__ propagated
- Django static files won't load
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.
- From django.db import models, migrations ImportError: cannot import name migrations
- Appropriate choice of authentication class for python REST API used by web app
- How to test login process?
- Passing a user, request to forms
- Django Forms: Foreign Key in Hidden Field
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
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
- Problem launching docker-compose : python modules not installed
- In Django, how can I get an exception's message?
- Getting error with is_popup variable in django 1.9
- Django crontab not executing test function
- What is Serializers to_internal_value method used for in Django
- How to create custom groups in django from group
- How to write an Ubuntu Upstart job for Celery (django-celery) in a virtualenv
- Django template to populate bootstrap rows and columns
- Django – Overriding get_form to customize admin forms based on request
What worked for me was to install django within my virtualenv. All other settings were correct in Visual Studio Code.
- Django object is not iterable using serializers.serialize
- Select Children of an Object With ForeignKey in Django?
- Make Django forms use comma as decimal separator
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 :
After selecting that path, Here your error get solved and it will removed orange wavy underlines.
- For windows OS
- Django – Change a ForeignKey relation to OneToOne
- AssertionError: The field ' ' was declared on serializer ' ', but has not been included in the 'fields' option
- Unable to install MySQL-python
- Reset SQLite database in Django
- Reliable way to only get the email text, excluding previous emails
You could as well add:
# pylint: disable=import-error
to the top of your file.
- Django bulk create objects from QuerySet
- Pagination in Wagtail
- Django: how to filter() after distinct()
- Django how to override clean() method in a subclass of custom form?
- Django: passing JSON from view to template
If you are using VScode, run:
then select Python: Select Interpreter
finally, select your current virtual environment directory from your project.
I hope it should be fixed.
- Remove app with Django 1.7 migrations
- Django querysets + memcached: best practices
- Haystack says “Model could not be found for SearchResult”
If you are using MacBook, follow the below steps:
Step 1: Open the search console by hitting command+shift+p
This should fix the error you are seeing.