2

I have installed python version 3.7 and visual studio code latest version, after that i have created virtual environment and installed django and created Django project.

Whenever i am opening some file, its showing error like below

unresolved import 'django.contrib' unresolved import 'django.urls' Undefined variable: 'path' Undefined variable: 'admin'

Below are the paths of my application

1) Python : C:\Python\Python37-32\python.exe 2) Virtual environment created D:\django_projects\envs\py1\ 3) Django Project created D:\django_projects\p1\p1

Below are the things i have tried 1) Re Installing Python 2) Setting the Python Path in environment variable even i selected include in the path at the time of python installation 3) Re installing the VS Code 4) Tried commenting the "python.jediEnabled": false, in settings.json file in vs code, but it was giving different error unable to import django. 5)

unresolved import 'django.contrib' unresolved import 'django.urls' Undefined variable: 'path' Undefined variable: 'admin'

enter image description here

martineau
  • 119,623
  • 25
  • 170
  • 301
vickey
  • 59
  • 2
  • 2
  • 5

4 Answers4

8

You did not select the virtual environment you installed Django into in VS Code (see the bottom-left corner for your screenshot where it says "Python 3.7.4 32-bit"; it would say "venv" or something if you were using a virtual environment). Try clicking on the interpreter in the status bar and then select your environment.

Brett Cannon
  • 14,438
  • 3
  • 45
  • 40
  • Thanks for your reply,but when i tried to include the Importmagic: index plugin in VS it get worked. this issue occurred the system is 64bit but in the screenshot its showing 32bit. – vickey Oct 02 '19 at 14:57
  • 1
    @vickey that would be because you installed 32-bit Python -- which is the default if you just clicked the "Download" button at python.org for Windows -- instead of the 64-bit version. – Brett Cannon Oct 02 '19 at 21:02
2

In my case, the error was:

unresolved import 'pydotplus' Python (unresolved-import)

And it was not a 64 bit vs. 32 bit issue. Instead, the wrong linting (because the code is running, and there is just wrong underlining in the editor) came from a needed extra python path in the json settings.

At best, following https://github.com/Microsoft/python-language-server/issues/887 and there the approach of HozcarAndres and the one after it.

//"python.pythonPath": "C:/Users/Admin/Anaconda3/python.exe",
"python.autoComplete.extraPaths": [
    "C:/Users/Admin/Anaconda3/Lib/site-packages/",
    ... (you can add further pahts in this String array)
    ]

"python.pythonPath" is not needed because it is the already known default.

Or go to settings.json (Ctrl+Shift+P and search for it) and change it to

{
    [many settings...],
    [previous last line],
    "python.pythonPath": "C:/Users/Admin/Anaconda3/**"
}

(or change the existing "python.pythonPath", though this is not there as a default)

Then, the packages like django which are only in the C:/Users/Admin/Anaconda3/Lib/site-packages/ will automatically be recognised by the linting, while as a default, the path is only C:/Users/Admin/Anaconda3/python.exe - not enough to "know" the site-packages. And you cannot make a list of paths here, as only a single string can be entered.

In case the Python interpreter gets lost after this, you can newly assign the python interpreter. Go to the left bottom in the blue line and choose "Python 3.7..." interpreter again.

Any more settings on the linting are here: https://code.visualstudio.com/docs/python/linting

questionto42
  • 7,175
  • 4
  • 57
  • 90
2

Use the following setting in your workspace settings .vscode/settings.json:

"python.autoComplete.extraPaths": ["./path-to-your-code"],

Or if you're using Pylance:

"python.analysis.extraPaths": ["./sources"]

Example

Consider the following directory

.
├── vscode
│   └── settings.json
└── src
    ├── main.py
    └── assets
        └──module.py

settings.json would need to contain (If using Pylance)

}
    "python.analysis.extraPaths": ["src/assets"]
}

And main.py needs something similar to

from module import *

Reference: Troubleshooting, Unresolved import warnings

Freddy Mcloughlan
  • 4,129
  • 1
  • 13
  • 29
1

On your Workspace vscode/setting.json simply place the "Python.autoComplete.extraPaths" to correspond to your Project File so that when you import a Module on your script, Your pyDev server can detect it and show no error to your Code. e.g "python.autoComplete.extraPaths": ["./path-to-your-script-code"],

enter image description here

Grayrigel
  • 3,474
  • 5
  • 14
  • 32