简体   繁体   中英

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 files 1 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?

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

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 )

    在此处输入图像描述

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}" },

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM