To fix the unresolved import error, set your Python path in your workspace settings. If you work with Visual Studio Code and import any library, you will face this error: “unresolved import”.
{
"python.pythonPath": "/path/to/your/venv/bin/python",
}
Then reload the VSCode, and it will fix that error.
For the Python-specific imports, it resolves the unresolved import issue, but it won’t resolve your modules. When importing your modules, it will return the same error, and to resolve this error, use the following setting in your workspace settings .vscode/settings.json.
"python.autoComplete.extraPaths": ["./path-to-your-code"],
When importing your python modules, which are in the workspace folder, but the main script is not in the root directory of the workspace folder, the imports are unresolved.
There is an alternative way that you can use the command interface.
Type the Cmd / Ctrl + Shift + P → Python: Select Interpreter → choose the one with the packages you are looking for, and that is it.
Using .env file
You can also create a .env file in your project root folder. Then add a PYTHONPATH to it like the following code.
PYTHONPATH = path/to/your/code
And in your settings.json file, add the following code.
"python.envFile": ".env"
Then reload the VSCode, and it will fix that error.
Hi Krunal,
Could you please explain where the workspace settings are so I can set this value you mentioned.
{
“python.pythonPath”: “/path/to/your/venv/bin/python”,
}