What's the purpose of "import package"?
- by codethief
As I just found out import package does not make the package's modules available through package.module. The same obviously holds true for from package import subpackage as well as from package import *
What's the purpose of importing a package at all then if I can't access its submodules but only the objects defined in __init__.py?
It makes sense to me that from package import * would bloat the namespace, which, however, doesn't apply in case of the other two ways! I also understand that loading all submodules might take a long time. But I don't know what these unwanted side-effects, "that should only happen when the sub-module is explicitly imported", are which the author of the previous link mentions.
To me it looks like doing an import package[.subpackage] (or from package import subpackage) makes absolutely no sense if I don't exactly want to access objects provided in __init__.py.
Are those unwanted side effects really that serious that the language actually has to protect the programmer from causing them? Actually, I thought that Python was a little bit more about "If the programmer wants to do it, let him do it."
In my case, I really do want to import all submodules with the single statement from package import subpackage, because I need all of them!
Telling Python in the init.py file which submodules I'm exactly talking about (all of them!) is quite cumbersome from my point of view.
Please enlighten me. :)