从父兄弟姐妹导入 Pyright 导致缺少导入

Pyright import from parent sibling results in missing import

我正在尝试 运行 一个 django 应用程序,这是以下结构(最小复制):

root/
  business/
    __init__.py
    urls.py
  monitoring/
    __init__.py
    api.py
  manage.py

urls.py 中,我有以下导入:from monitoring.api import MonitoringConfigurationAPIView。但是,这会导致 Pyright 错误 [Pyright reportMissingImports] [E] Import "monitoring.api" could not be resolved.

添加说明符以查看父目录 (from ..monitoring.api ...) 会使 Pyright 静音,但会导致 Python 本身出错:

File "/home/user/Projects/root/business/urls.py", line 20, in <module>
    from ..monitoring.api import MonitoringConfigurationAPIView
ImportError: attempted relative import beyond top-level package

我试过向根添加空白 __init__.py,但这没有帮助。我尝试在根目录 (root/pyrightconfig.json) 中创建一个 pyrightconfig.json 文件并设置根目录,但这也没有用。

{
    "executionEnvironments": [
        {
          "root": "."
        }
    ]
}

它可以很好地处理所有外部包,没有问题。只有本地才是问题。我错过了什么吗?

找到一个 link 指出了我的问题。 pyrightconfig.json 必须位于 repo 根目录,而不是项目根目录。

Just solved my issue. It seems the coc extension recognizes the root of your git repo as the root not the directory you open. I was using a subdirectory of a monorepo as my root and opening that with neovim on it's own. I just moved the pyrightconfig.json to the correct root and added the prefix of my sub-directory to the root path in an execution environment and it worked.

https://github.com/fannheyward/coc-pyright/issues/235#issuecomment-756288634

我的回购在技术上是

repo/
  namespace/
    namespace/
      django root/
        business/
        ...

虽然我认为这不重要。我的错。