72

I know of these two questions about __init__.py and __main__.py:

What is __init__.py for?

What is __main__.py?

But I don't really understand the difference between them. Or I could say I don't understand how they interact together.

buhtz
  • 10,774
  • 18
  • 76
  • 149

2 Answers2

107

__init__.py is run when you import a package into a running python program. For instance, import idlelib within a program, runs idlelib/__init__.py, which does not do anything as its only purpose is to mark the idlelib directory as a package. On the otherhand, tkinter/__init__.py contains most of the tkinter code and defines all the widget classes.

__main__.py is run as '__main__' when you run a package as the main program. For instance, python -m idlelib at a command line runs idlelib/__main__.py, which starts Idle. Similarly, python -m tkinter runs tkinter/__main__.py, which has this line:

from . import _test as main

In this context, . is tkinter, so importing . imports tkinter, which runs tkinter/__init__.py. _test is a function defined within that file. So calling main() (next line) has the same effect as running python -m tkinter.__init__ at the command line.

Two-Bit Alchemist
  • 17,966
  • 6
  • 47
  • 82
Terry Jan Reedy
  • 18,414
  • 3
  • 40
  • 52
  • 2
    Why does `tkinter` have a lot of code in the `__init__.py` file while `idlelib` have no code there? Is there a reason why you would sometimes want to have code in `__init__.py` but other times not? – HelloGoodbye Aug 11 '17 at 10:59
  • 3
    Code in `package/__init__.py` makes the `import package` actually do something. Before 3.0, `Tkinter` was a module, not a package. In 3.0, the code in `Tkinter.py` was put in `tkinter/__init__.py, so the only change needed was `import tkinter` instead of `import Tkinter`. – Terry Jan Reedy Aug 13 '17 at 22:52
  • @Terry Jan Reedy: Could you please explain "So calling main() (next line) has the same effect as running python -m tkinter.__init__ at the command line" ..? Thanks. – Learnerer May 07 '22 at 20:02
  • @HelloGoodbye `tkinter` a package but the devs wanted `import tkinter` to give access to the pre-ttk widget set. Sticking the code for this in __init__ was the easiet way to accomplish this. – Terry Jan Reedy May 10 '22 at 01:14
12

__init__.py, among other things, labels a directory as a python directory and lets you set variables on a package wide level.

__main__.py, among other things, is run if you try to run a compressed group of python files. __main__.py allows you to execute packages.

Both of these answers were obtained from the answers you linked. Is there something else you didn't understand about these things?

Jeff H.
  • 320
  • 1
  • 6