Python Package

A package is a directory containing python modules (or files) containing related set of code.

__init__.py

__init__.py is a file in a package which is added to make a directory a package. With old versions of python, it was mandatory to add this file. With newer versions, it is optional. However, for best practices, we should add it.

Features

  1. It can used to execute some code when the module is imported.
  2. When importing everything using from package import *, __init__.py can decide what all to import. By default nothing is imported when does like it. We need to explicitly add __all__ global variable in __init__.py.

__init__.py

__all__ = ["format", "my_math"]

Tip

Although, above can be accomplished using from .format import *.

__init__.py

from .format import *
from .math import *
from lib import *
 
line(10)
# '----------'