环境:
- 蟒蛇 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 引起的,但这就是我希望它设置的方式。有人对此有解决方案吗?