Skip to content Skip to sidebar Skip to footer

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

Solution 1:

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.


Solution 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


Solution 3:

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


Solution 4:

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


Post a Comment for "Visual Studio Code Unresolved Import?"