When does code in __init__.py get run?

Solution 1:

The code in __init__.py is run whenever you import anything from the package. That includes importing other modules in that package.

The style of import (import packagename or from packagename import some_name) doesn't matter here.

Like all modules, the code is run just once, and entered into sys.modules under the package name.