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
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
For me I would prefer after that to do something like that
but instead of that I have to write
Ok we can add
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 (