6

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

2
  • Python is and isn't about "if the programmer wants...". I have implemented some pretty horrible logic with Python but some debatable bad ideas like assignment in conditional expressions is strictly prohibited.
    – David
    Commented Apr 13, 2010 at 11:02
  • But still, it's a bit about giving the programmer as much freedom as possible, isn't it? There are no strict rules, there are conventions about what should be done and what should better not be done. Just consider that there are no access modifiers like private / protected / package / public. Instead, there's just the rule of thumb that you better not mess around with class / object attributes starting with an underscore. But you can if you want and in some cases of which debugging is not the least it's really useful.
    – balu
    Commented Apr 13, 2010 at 11:58

2 Answers 2

4

It's up to the package if it automatically imports submodules in its __init__.py or not. For example os automatically imports os.path, so this works:

import os
print os.path.abspath('somefile')

So the creator of the package can decide what is best. If automatically loading all submodules would take a rather long time and would rarely be needed, it probably shouldn't be done. If all the submodules are always required they probably should be imported automatically.

If you think a package imports not enough you could work around it by writing your own wrapper module that does all the imports, and then use that with from wrappermodule import *.

2
  • 1
    Adding to this, in summary import package allows more choices to the package designer. At minimum it provides name-spacing but if warranted or desired import package can add functions, classes, and sub-packages to itself. The extra functionality is there if you need it but not forced or required.
    – David
    Commented Apr 13, 2010 at 10:59
  • The only fact that's bugging me, now, is that there's practically no way I could automatically import all submodules. (Besides the solution by Roger Pate below that I'd rather consider a hack.) That somehow limits the "freedom" both of you mention. Yet, I see your point that the package designer should make that decision.
    – balu
    Commented Apr 13, 2010 at 12:04
1

As said, the solution is to import each submodule. You can use a hack to avoid listing them:

__init__.py

import os

def import_sub():
  for fn in os.listdir(os.path.dirname(__file__)):
    if fn.endswith(".py") and fn != "__init__.py":
      __import__(".".join([__name__, os.path.basename(fn)[:-3]]), level=0)
import_sub()
del import_sub  # if desired

This will easily break under some circumstances. However, it will handle the common case and could be temporarily suitable.

1
  • Although it's rather inelegant, I might use it. Thanks!
    – balu
    Commented Apr 13, 2010 at 12:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.