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.
and when I use the same command inside my virtual environment (myDjangoEnv)
Fix:
- Now what I understood is that pylint uses the python interpreter to analyze our code and mark the error while we write the code.
- 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:
14๐
Follow steps mentioned in the image. For details, or if that doesnโt work, read further!!!
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-
-
To check the path, activate venv and type
which python
in terminal, this will give path. Copy the path. -
Click interpreter on lower left, to pull drop-down, as shown in pic above.
-
Click enter the interpreter path.
-
Paste path copied.
This will assign the right interpreter and rectify the error.
- [Django]-Use django: from "python manage.py shell" to python script
- [Django]-Interactive Graphviz graphs in a web application
- [Django]-Django set range for integer model field as constraint
9๐
-
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
-
Copy the address of your virtual environment.
-
On VS Code, Select File > Preferences> Settings to open your User Settings (or use the Ctrl+, shortcut).
-
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
- [Django]-Django ImageField change file name on upload
- [Django]-Capture parameters in django-rest-framework
- [Django]-Can a dictionary be passed to django models on create?
5๐
-
Ctrl+Shift+P
-
Type Python:select interpreter
Now we will get: -
Choose Enter interpreter path:
-
Select
Find...
Then
- [Django]-How to update user password in Django Rest Framework?
- [Django]-'module' object has no attribute 'now' will trying to create a CSV
- [Django]-How can I embed django csrf token straight into HTML?
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
- [Django]-"Too many SQL variables" error in django with sqlite3
- [Django]-Django Rest Framework: turn on pagination on a ViewSet (like ModelViewSet pagination)
- [Django]-Python vs C#/.NET โ what are the key differences to consider for using one to develop a large web application?
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.
- [Django]-Django-admin: Add extra row with totals
- [Django]-Sending JSON using the django test client
- [Django]-Django update table using data from another table
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 ๐
- [Django]-Bulk update for many to many fields
- [Django]-Django content-type : how do I get an object?
- [Django]-MySQL ERROR 2026 โ SSL connection error โ Ubuntu 20.04
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.
-
workon [yourEnvName]
-
pip install pylint
-
pip install django
-
pip install djangorestframework
and so onโฆfor all the modules you want to use.
- [Django]-Initial populating on Django Forms
- [Django]-How to quote a string value explicitly (Python DB API/Psycopg2)
- [Django]-Django staticfiles app help
- [Django]-Django: Add response header when using render or render_to_response
- [Django]-Django: How can I create a multiple select form?
- [Django]-How to write a query to get find value in a json field in django
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.
- [Django]-Setting DEBUG = False causes 500 Error
- [Django]-Overriding initial value in ModelForm
- [Django]-Django class-based views with inline model-form or formset
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.
- [Django]-How to update fields in a model without creating a new record in django?
- [Django]-Django DetailView โ how to use 'request' in get_context_data
- [Django]-Atlassian Bamboo with Django & Python โ Possible?