I have the following package structure
package __init__.py sub1 __init__.py foo.py # Contains class Foo sub2 __init__.py bar.py # Contains class Bar
I want to be able to just import package and have package.Foo and package.Bar , i.e. I want subpackages to be transparent to users.
The trick is that importing sub2 is time consuming and many users don’t care about things in sub2 at all and only want things in sub1. Thus, I want users to be able to say import package.sub1 or from package import sub1 only import sub1 and skip import sub2.
I know I can reach the first part if package/__init__.py contains
from .sub1 import * from .sub2 import *
and has package/sub1/__init__.py be from .foo import Foo and similarly for sub2. However, this will always import sub1 and sub2, even if the user only tries to import package.sub1 .
Accordingly, I can achieve the second part by getting package/__init__.py empty and using the same sub1/__init__.py as above. However, simply saying import package does not load sub1 or sub2, so users will need to explicitly load them, and then refer to package.sub1.Foo .
Ideally, the solution will work in both 2.7.10 and 3.5.0, but I will agree with one or another if both options are impossible.
source share