Federico Malerba
Federico Malerba

Reputation: 815

VS Code Pylint unresolved import on not yet installed package

The structure of my package is the following:

package
|___package
|        |_subpackage
|        |            |_ __init__.py
|        |            |_ module_Y.py
|        |_ __init__.py
|        |_ module_X.py
|_ main.py
|_ setup.py

My __init__.py files are all empty and module_Y.py has the line from package import module_X.

I have not yet installed the package since it's not even remotely close to be working, but I want Pylint to be able to understand that the import statement in module_Y.py is going to be correct. I know that this must be possible because cloning the repo of TF-Agents and opening it in VS code, pylint understand the references inside the files1 even if I have not yet installed the TF-agents repo.

I know that I could use relative imports like from .. import module_X, and I know that I could just disable these pylint warnings, but these two me are half solutions. The first is not as clean and clear as the statement from package import module_X and the second solution possibly doesn't tell me of something being actually wrong.

What am I missing?

1Take for example tf_agents/agents/dqn/dqn_agent.py which is able to resolve the imports to tf_agents.policies

Upvotes: 0

Views: 237

Answers (1)

Jill Cheng
Jill Cheng

Reputation: 10344

According to your description, I reproduced this problem, the following is my solution and you could refer to it:

Way 1:

  1. Please add the following code at the beginning of the file "module_Y.py", which adds the file path to the upper level directory "package":
import sys
sys.path.append("..")
  1. Please set "cwd": "${fileDirname}", in "launch.json";

  2. Click F5 to debug the code: (Since this warning does not affect the use of the code, we can close it after the code is available: use "python.analysis.disabled": [ "unresolved-import" ], in settings.json )

    enter image description here

Way 2:

Or you could also set in "launch.json": (It adds the folder path of the current workspace to the system path.)

"env": {
                "PYTHONPATH": "${workspaceFolder}"
            },

enter image description here

Upvotes: 1

Related Questions