Tim
Tim

Reputation: 563

Import with calls out and from a folder

I made the following architecture:

parent/
      main.py
      tool/
        __init__.py
        base.py
        view.py

base.py and view.py hosts the classes of the same name.

__init__.py
from .base import Base
from .view import View
view.py
from .base import Base
main.py
from tool import View
    from .base import Base
ImportError: attempted relative import with no known parent package

So if I change from .base import Base to from base import Base in view.py, python tool/view.py works but python main.py fails with:

    from base import Base
ModuleNotFoundError: No module named 'base'

What import should be made to make both work?

python main.py and python tool/view.py

Thanks for your help.

Upvotes: 1

Views: 141

Answers (1)

enzo
enzo

Reputation: 11486

Your first imports are fine, you should just change the second command to

python -m tool.view

Upvotes: 2

Related Questions