'VS Code cannot resolve imports from submodule

I am working with git submodules and would like to jump to imported Python files with "Ctrl+Click". The problem is that the submodule's files reference another root directory that they import from than the base module. I therefore added the submodule's root directory to the VS Code workspaces, which I believe has resolved a similar issue of mine once before and made VS Code recognize the imports, but this time it does not work.

The situation is illustrated below:

file_in_submodule.py:

from folder_in_submodule_root.another_folder.python_file_to_import_from.py import PythonClass

Error message:

Import "folder_in_submodule_root.another_folder.python_file_to_import_from" could not be resolved Pylancereport(MissingImports)

Any solution? Thank you.



Solution 1:[1]

I solved the problem by hovering over the import line. It showed a lightbulb and the option to add various paths to the workspace.

VSCode created a file called workspace.code-workspace in folder_in_submodule_root.another_folder.

workspace.code-workspace:

{
    "folders": [
        {
            "path": "../../../../../../../../../.."
        },
        {
            "path": "../../.."
        }
    ],
    "settings": {}
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 naraghi