AppDividend
Latest Code Tutorials

Python Unresolved Import: How to Solve Pylint Error

0

If you have just started coding Python in VS Code, make sure the python environment has been set. If you have not set the python environment, VSCode couldn’t detect the specific python executor. Let’s see how to solve the “unresolved import” error.

Python Unresolved Import

If you are working with Visual Studio Code and import any library, you will face this error: “unresolved import”. To resolve this error, In your workspace settings, you can set your Python path like the following.

{
    "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 own modules. When importing your own 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 own 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 look 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.

That is it for this error fixing tutorial.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.