2

I am checking the scikit-learn repository in GitHub to understand how this package is organised. I noticed in all sub-packages (and the main package) __init__.py file contains a set of import statements and then all those modules (that have been imported earlier) are assigned to __all__. (For instance, take a look at https://github.com/scikit-learn/scikit-learn/blob/main/sklearn/metrics/init.py)

I know that by importing a packages (sub-package) all instructions in __init__.py are executed, and all modules indicated in __all__ list are imported. So, I cannot understand why all the packages that have already been imported are also assigned in __all__. It seems redundant to me. It would be great if someone can clarify this to me (for example, what would happen if we remove __all__ from __init__.py file in the example).

amiref
  • 3,181
  • 7
  • 38
  • 62
  • 2
    https://stackoverflow.com/questions/44834/what-does-all-mean-in-python – CristiFati Jul 26 '22 at 22:01
  • @CristiFatiI I know what `__all__` means in `__init__.py`. I do not understand why in the `__init__.py` files in scikit-learn, modules are imported and then the same modules are assigned to `__all__` – amiref Jul 26 '22 at 22:05
  • 1
    Re-read your question and I think I understand it now. scikit-learn's [coding guidelines](https://scikit-learn.org/dev/developers/develop.html#coding-guidelines) make an offhand reference to Pyflakes. [This answer to another question](https://stackoverflow.com/a/19085245/3004881) might be the reason. – Dan Getz Jul 27 '22 at 00:22
  • Indeed, it satisfies the linter's question "why are you importing this but not using it"? In the case of listing an imported name in `__all__` it's a valid "use" of the import. – Iguananaut Jul 27 '22 at 01:46

0 Answers0