Best practice for handling submodule imports

Hi everyone,

I’m having a fight between my linter pyright and a streamlit module error. Here’s my project structure:

project_folder
    | --> Pipfile, etc
    | --> project
            | --> __init__.py
            | --> base.py
            | --> submodule
                    | --> __init__.py
                    | --> base2.py

My main entrypoint is in base.py, which I call with streamlit run project/base.py with my terminal located in project_folder. But when I run the script and try to import a function from base2, I get the following errors:

Using

from project.submodule.base2 import X

Error:

ModuleNotFoundError: No module named 'project'

Using

from .submodule.base2 import X

Error:
ModuleNotFoundError: No module named '__main__.submodule'; '__main__' is not a package

However, if I break the linter convention and perform a relative import with:

from submodule.base2 import X

my module loads successfully and the streamlit page runs.

Could you advise on best practice regarding how to deal with imports? Tested this with 0.73.0 and 0.74.1. All scripts were working yesterday, not today.

1 Like