How to Fix the Pylint Unresolved Import Error in Python

  1. Fix the Pylint unresolved import Error in Python
  2. Use the .env File
How to Fix the Pylint Unresolved Import Error in Python

If we have just started coding Python in Visual Studio Code, ensure the Python environment has been set. If we have not set the Python environment, Visual Studio Code couldn’t detect the specific Python executor.

This article will discuss solving the unresolved import error with Pylint in Python.

Fix the Pylint unresolved import Error in Python

To solve the unresolved import error in Python, set your Python path in your workspace settings. For example, if we work with Visual Studio Code and import any library, we will face this error: unresolved import.

{
    "python.pythonPath": "/path/to/your/venv/bin/python",
}

Then reload the Visual Studio Code to fix the error.

It resolves the unresolved import issue for the Python-specific imports, but it won’t fix your modules. Instead, when importing your modules, it will return the same error, and to fix this error, use the following setting in your workspace: .vscode/settings.json.

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

The workspace settings file is in your root folder under the .vscode folder.

Workspace settings.json location

The imports are left unresolved when you import your Python modules from the workspace folder, but the main script is not in the workspace folder’s root directory.

We can also use the command interface in another manner.

Enter Cmd or Ctrl+Shift+P followed by "Python: Select Interpreter" and then choose the one with the packages we look for. To know more about the Visual Studio Code workspace settings, you may refer to the official documentation that can be found in this link.

Use the .env File

We 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 Visual Studio Code to fix the error.

Marion Paul Kenneth Mendoza avatar Marion Paul Kenneth Mendoza avatar

Marion specializes in anything Microsoft-related and always tries to work and apply code in an IT infrastructure.

LinkedIn

Related Article - Python Error