What's the purpose of "import package"?

Posted by codethief on Stack Overflow See other posts from Stack Overflow or by codethief
Published on 2010-04-13T10:30:42Z Indexed on 2010/04/13 10:32 UTC
Read the original article Hit count: 376

Filed under:
|
|

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. :)

© Stack Overflow or respective owner

Related posts about python

Related posts about import