Importing adjacent modules in Python3

Viewed 53

Assume the folder structure is

src
  -__init__.py
  -A
    -__init__.py
    -lib.py
  -B
    -__init__.py
    -script.py

I am in script.py trying to import a function p() from A.lib. The file script.py contains

import os
print(os.listdir())
from A.lib import fkt

if __name__ == "__main__":
  fkt()

I am calling os to show that my current working directory is in src/ so the module A should be in reach. The return from the print statements is

['A', '.vscode', 'B', '__init__.py']

Is there a "correct" way to do this? I figure importing the path via sys isn't "correct" so I'd like to avoid doing this.

Note: I have tried almost every version of from src.A.lib, from .A.lib, or from A.lib and all of these return either that src is not a package or that there is no module A.

OS: I am running on ubuntu 18.04 LTS with python 3.6.9 using vscode with "cwd" set to "${workspaceFolder}".

0 Answers
Related