I was trying to following the design pattern shown in this previous question related to SQLAlchemy and intended to share a common Base instance across multiple files. The co
One solution I found is to:
import base
lines in a.py, b.py, and c.py to from . import Base
from model import base
to from model import Base
base.Base
to Base
.I'm still uncertain why the previous design works in python2 but not python3. The changes above make it work in both python2 and python3.
Python 3 switches to absolute imports by default, and disallows unqualified relative imports. The from base import Base
line is such an import.
Python 3 will only look for top-level modules; you don't have a base
top-level module, only model.base
. Use a full module path, or use relative qualifiers:
from .base import Base
The .
at the start tells Python 3 to import starting from the current package.
You can enable the same behaviour in Python 2 by adding:
from __future__ import absolute_import
This is a change introduced by PEP 328, and the from future
import is available from Python 2.5 onwards.