Chuck Chuck - 2 months ago 5
Python Question

What do I need to place in my `` files to simplify this package's interface?

I have a package that works with the following files:

# package/sub1/
class Obj1:

# package/sub2/
from ..sub1.obj1 import Obj1
class Obj2(Obj1):

Currently, with a file at the same level as the
folder, I need to do this:

from package.sub1.ob1 import Obj1
from package.sub2.ob2 import Obj2

I want to be able to use the following:

from package.sub1 import Obj1 # can reference package.sub1.Obj1
from package.sub2 import Obj2 # can reference package.sub2.Obj2
from package import * # Can reference both

I know I need to edit one or more of the three existing
files, and I think I'll need to set the
variable in one or more of them, but I can't seem to figure out what the proper use is for what I want. Am I going about this incorrectly?


You could make it work by adding the following to your files:

In package/sub1/

from .ob1 import Obj1

In package/sub2/

from .ob2 import Obj2

In package/

__all__ = ['sub1', 'sub2']

Now top-level code can do any the imports you asked about and they'll make package.subX.ObjX a valid name.

Is this a good design? Not really. One of the principles of good Python design is that "flat is better than nested". As user2357112 commented, unlike some other languages, Python doesn't require each class to have its own file. Since you want the Obj1 and Obj2 classes to be accessible from the package.sub1 and package.sub2 namespaces, respectively, it might make more sense to combine the files from the subX folders into a single file (replacing the subpackage with a single larger module).