Import all names from Python files in package (from * import *)

If you find yourself writing something like this in your __init__.py for each file you add to the package

from foo import *
from bar import *

I can give you small code snipped how to automate that.

But first - why do we write something like that at all? In many cases this is just bad style - you mix a lot of names in the namespace and that can be dangerous. So in general you better avoid that.

But what about such a design

- model.py
    - user.py
        class User(Base):
            ...
    - order.py
        class Order(Base):
            ...

For me I would prefer after that to do something like that

- controllers.py
    - add_user.py
        from model import User, Order
        def add_user(user: User, order: Order):
            ...

but instead of that I have to write

- controllers.py
    - add_user.py
        from model.user import User
        from model.order import Order
        def add_user(user: User, order: Order):
            ...

Ugly?

Ok we can add __init__.py:

- model.py
    - __init__.py
        from .user import *
        from .order import *
    - user.py
        class User(Base):
            ...
    - order.py
        class Order(Base):
            ...

But this is just boring to write all this import for each model we add.

Stop! This is Python so you can automate all what you want!

This little code snippet solves exactly this task - how to from * import *.

On line 8 we iterate all files in the folder using Path.globe().

On line 9 we check this is not subfolder and the name is not starts with underscore (so we exclude __init__.py and all other files you give such a special names).

On line 10 we import the file as Python module using for that relative package name (see dot at the beginning?).

Line 11 gets list of all non-magic names from the module.

And finally on line 12 we add all this names to the current namespace (__init__.py).