简体   繁体   中英

visual studio code unresolved import?

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'

在此处输入图片说明

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 .

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

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

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. eg "python.autoComplete.extraPaths": ["./path-to-your-script-code"],

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM