Search code examples
pythonpython-importpython-module

Python module organization to make import statement cleaner


I have a directory B inside directory A, which resides in a directory included in PYTHONPATH.

Now lets say that within directory B i have files - B_file_1.py, B_file_2.py, with each file defining a single function (i.e. B_file_1.py defines B_file_1).

I want to use this collection of files as a python package. But now when I want to use the method B_file_1, I have to write this long statement:

from A.B.B_file_1 import B_file_1

What I would like is to have the convenience of simply writing this instead, (while maintaing the directory and file setup I currently have):

from A.B import B_file_1

Is there any python module hack to do this?


Solution

  • Add this code in A/B/__init__.py:

    from B_file_1 import B_file_1