Python relative imports that skip intermediate folder in VSCode

120 views Asked by At

I am trying to understand the code in someone else's mixed-language repository (part of a large, complicated code base). The directory structure is like this:

module_A/
    python/
        __init__.py
        script_A.py
        script_B.py
    other/
        other_scripts.cxx

The contents of __init__.py are

__version__ = '1.0.0'

The code has relative imports from script_B.py to script_A.py like the following:

from module_A import script_B

Note that the intermediate directory python has been skipped. I know that the code runs, so python itself does not have a problem parsing this import statement. However, in VSCode, pylint throws a fit:

No name 'script_B' in module 'module_A'

I would really like to resolve this error so that (a) I can use VSCode tools to help myself understand how the code works (e.g., function definitions on hover) and (b) I can more easily contribute to the code in the future (e.g., with code autocomplete).

Can someone explain the following?

  1. What is going on here? I have never seen relative imports in python that skip an intermediate directory before.

  2. How can I resolve the error in VSCode to recover the usual quality-of-life tools?

1

There are 1 answers

2
MingJie-MSFT On

script_A.py is in the folder python. If your workspace is the parent directory of module_A, you have to use the codes like from module_A.python import script_B.