I asked a similar question yesterday, but have acquired a really odd problem since then. With this directory structure:
app/
models/
__init__.py
user.py
other.py
pages/
__init__.py
pages.py
The models/__init__.py file has this line:
__all__ = ['user', 'other']
and the pages/__init__.py has
__all__ = ['pages']
In pages.py, in order to use any of the classes in user.py or other.py, I have to have
from models import *
import models
at the top, and then I can declare a User class like this:
my_user = models.user.User()
If I exclude either of the import-ing statements at the top, I get errors like
"Class user has no attribute User"
Any help would be appreciated. I love Python, but I wish it’s import functionality worked more like PHP’s.
There are two options, depending on where you want to be explicit and how much you want available “by default” (which also means forced).
In those __init__ files you could use:
Or, where you would otherwise just have
import models, use:The latter form is more widely preferred.