I am trying to build a python package. After reading documentation and other tutorials about modules and packages, I am still a bit confused about how limit the modules/packages exposed on the public interface of the root package (meaning the stuff that the user can import and use with the package).
For example, I have a sample project structured like the following:
├── README.md
├── main_package
│ ├── __init__.py
│ ├── config.py
│ ├── main.py
│ ├── sub_package_1
│ │ ├── __init__.py
│ │ └── sub1.py
│ └── sub_package_2
│ ├── __init__.py
│ └── sub2.py
├── package.json
└── setup.py
main_package/main.py
from .config import Config
from .sub_package_1 import sub1_func
from .sub_package_2 import sub2_func
def main_func():
print(Config.DUMMY, sub1_func(), sub2_func())
main_package/__init__.py
from .main import main_func
main_package/sub_package_1/sub1.py
def sub1_func():
return 'sub1_func'
main_package/sub_package_1/__init__.py
from .sub1 import sub1_func
main_package/sub_package_2/sub2.py
def sub2_func():
return 'sub2_func'
main_package/sub_package_2/__init__.py
from .sub2 import sub2_func
What I would like to know if there is a way to selectively control what will be exported (visible) on the public interface of the main package. Let's say for this example I want to export only main_func
from main_package.main
and the subpackage sub_package_1
and anything else. Can I do that?
from main_package import main_func # OK
from main_package import config # NO!
from main_package.sub_package_1 import sub1_func # OK
from main_package.sub_package_2 import sub2_func # NO!
It seems I am unable to do so. Every time I build the package, I can import both the sub packages and also the config.py
module which I don't want at all.