Question

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?

Was it helpful?

Solution

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

from B_file_1 import B_file_1

OTHER TIPS

Yes. You can import your modules inside of the __init__.py file in the B folder:

__init__.py:

__all__ = ('B_file_1',)

from B_file_1 import B_file_1

Now, your second import statement will work.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top