bonjour,
Sur un projet existant que je rejoins, le fichier __init__.py
contient l’import des différents modules.
D’après mes ressources, il serait préférable de laisser le __init__.py
vide :
-
C’est ce que l’on trouve dans le Hitchhiker’s Guide
-
Et dans cette vidéo de Brandon Rhodes à la conférence code::dive 2019 : When Python Practices Go Wrong (44e minute)
Voici ce qu’il dit :
__init__.py
is a cost in the way of all the other modules that you might want to import.Worse yet, for convenience, some
__init__.py
files import all their package’s module. That’s ruining the ability to cherry pick one thing you want. You can’t touch the package without waiting to load everything inside of it.My advice, which is not what everyone would say, is to keep
__init__.py
empty of codeI do understand that my users don’t want to have to import things manually from all dozen of my modules, so I have “an API module” that import all of the important classes so that you can say :
from skyfield.api import xxx, yyy, zzz
J’ai retrouvé le fichier où il met ses imports : python-skyfield/skyfield/api.py at master · skyfielders/python-skyfield · GitHub
- Cependant, un article très récent de RealPython propose exactement l’inverse. Est-ce qu’il y a des choses qui auraient changé ?
Quel est votre avis ?
Je préfèrerai partir sur un __init__.py
vide et cela m’intéresse de mettre les éventuels imports dans un fichier à part mais j’ai peur que ça me mette le bazar pour la documentation Sphinx (et sphinx-apidoc mais je vois un exclude pattern)
Merci d’avance pour vos conseils.
Françoise