Table of Contents

About

A regular package is typically implemented as a directory containing an __init__.py file.

The __init__.py file is tells Python that you can use the folder to import a module. (The default behavior is no import).

When a regular package is imported, this __init__.py file is implicitly executed, and the objects it defines are bound to names in the package’s namespace.

The __init__.py file can contain the same Python code that any other module can contain.

The __init__.py mechanism is done to prevent directories with a common name, such as string, from unintentionally hiding valid modules that occur later on the module search path.

In the simplest case, __init__.py can just be an empty file, but it can also:

  • execute initialization code for the package
  • or set the all variable

all

The __all__ variable define a list of module to import when using the package import * statement. See Python Package - Import It defines an explicit index of the package.

Example:

__all__ = ["echo", "surround", "reverse"]