Zazaeil
Zazaeil

Reputation: 4109

Pythonic way to split project into modules?

Say, there is a module a which, among all other stuff, exposes some submodule a.b. AFAICS, it is desired to maintain modules in such a fashion that one types import a, import a.b and then invokes something b-specific in a following way: a.b.b_specific_function() or a.a_specific_function().

The questions I'd like to ask is how to achive such effect? There is directory a and there is source-code file a.py inside of it. Seems to be logical choice, thought it would look like import a.a then, rather than import a. The only way I see is to put a.py's code to the __init__.py in the a directory, thought it is definitely wrong...

So how do I keep my namespaces clean?

Upvotes: 1

Views: 95

Answers (1)

goodside
goodside

Reputation: 4629

You can put the code into __init__.py. There is nothing wrong with this for a small subpackage. If the code grows large it is also common to have a submodule with a repeated name like a/a.py and then inside __init__.py import it using from .a import *.

Upvotes: 1

Related Questions