Search code examples
visual-studio-codepylance

Avoid warning from module in different folder with Pylance in VSCode


I have the following folder structure:

/mypythonproject
../modules
....mycustomfunctions.py
../script
....script.py

Inside the file script.py I declare the following:

import sys
sys.path.insert(1, '../modules')
from mycustomfunctions import function_a

script.py work just fine, but Visual Studio Code shows a warning on this:

from mycustomfunctions import function_a

Telling me that: Import "mycustomfunctions" could not be resolved

Is there something I can improve?


Solution

  • It seems that Pylance "prefers" the dot notation with __init__.py. In my example, one should create a blank __init__.py file inside the /modules folder:

    /mypythonproject
    ../modules
    ....__init__.py
    ....mycustomfunctions.py
    ../script
    ....script.py
    

    And then, in script.py, import the module as follows:

    from mypythonproject.modules.mycustomfunctions import function_a
    

    This is not showing any warning in the editor and works as expected.