为什么pylance无法解析本地相对路径导入(VSCode)?

时间:2021-03-01 15:37:18

标签: python-3.x python-3.6 pylance

环境:

  • Python 3.6.9
  • VSCode
  • 皮兰斯

我在 VSCode 中工作区的结构是:

.../sync
├── README.rm
├── script-program-1
│   ├── lib
│   │   ├── __init__.py
│   │   └── queries.py
│   └── sync.py
├── lib
│   ├── __init__.py
│   ├── config.yaml
│   ├── logging.yaml
│   └── sync_config.py
└── requirements.txt

我已将 "python.analysis.extraPaths": ["./lib"] 添加到位于 sync\.vscode 的 settings.json。

问题是在sync.py 脚本中,我在导入lib.queries 时遇到了问题。 如果我尝试 from .lib.queries import *,pylance 不会抱怨,但会失败并显示错误 attempted relative import with no known parent package

如果我改为使用 from lib.queries import * python3 按预期工作但 pylance 抱怨说 Import "lib.queries" could not be resolved(PylancereportMissingImports)

我可以更改什么才能使导入工作并且 pylance 没有问题?

更新 如果我将 ./script-program-1 添加到 settings.json python.analysis.extraPaths 问题就会消失。我遇到的问题是这个项目可能有数百个“script-program-X”文件夹,并且所有这些文件夹都可能有自己的本地导入文件。在我使用过的 VSCode 机器上,为每个人更新这个 extraPaths 变量并不理想。

我尝试过使用像“./${relativeFileDirname}”这样的变量以及像“./**”和“./*”这样的通配符,但这些都不起作用。

我知道这个问题是因为工作区根是同步文件夹而不是 script-program-1 引起的,但这就是我想要的设置方式。有人有解决办法吗?

0 个答案:

没有答案