Python – __init__.py

Whenever you play with a python package, you will always see some boilerplate files or structures here and there. __init__.py is a file that resides in most of the packages. And even sometimes, they are empty, so what they do?

Here is a summary of its functionalities:

1. It will treat the directory where it is as a loadable module.
Here is a real world example showing you what difference even an empty __init__.py makes.

2. You can define __all__ variable in __init__.py to decide when user type in from package import *, which modules will be loaded.

Screenshot 2015-01-03 11.18.34

3. Define a commonly used variable, check out this Stackoverflow answer.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s