Home > OS >  Grouping imports together from subdirectories
Grouping imports together from subdirectories

Time:10-06

Say I have a django app where I'm using folders and subfolders to organize my models:

app
    models
        __init__.py
        accounts
            user.py
            profile.py
        social
            facebook.py
            twitter.py
            linkedin.py
    admin.py
    apps.py
    urls.py
    views.py

My __init__.py file is as follows:

from .accounts.user import User
from .accounts.profile import Profile

from .social.facebook import Facebook
from .social.twitter import Twitter
from .social.linkedin import LinkedIn

Is there any way to group these imports together or make the code shorter?

E.g. (obviously doesn't work)

from . import *

# or

from .accounts import *
from .social import *

CodePudding user response:

Nope. The package that contains a module will not necessarily know anything about the contents of the module, so in your case accounts doesn't know anything about the User class in accounts.user. There isn't any general way to group things more than what you're already doing.

  • Related