why does python require __init__.py to treat directories as containing packages?
Question: While going through 6.4 Packages section of [python
manual](http://docs.python.org/2/tutorial/modules.html) I came across the
following line:

> The `__init__.py` files are required to make Python treat the directories as
> containing packages; this 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.

I understand that `__init__.py` is required to mark directories as containing
packages, but I don't understand what it means by `prevent directories with a
common name...from unintentionally hiding valid modules...`.

Could someone explain why the `__init__.py` is required?


Answer: Lets say you had a project that contained a directory called `math` that
contained some numerical data. If no `__init__.py` were required, then when
you did `import math`, it would try to import that directory instead of the
real `math` module. But since your directory just contained data and not
actual Python code, the import would fail. Thus your `math` directory would
block you from importing the real `math` module from the standard library,
even though your `math` directory doesn't contain Python code at all.

The `__init__.py` is like a confirmation, the directory saying "Yes, I really
am a Python package, not just a directory full of files. It makes sense to
import me." Any directories that don't "announce" themselves in this way are
skipped over because Python knows they can't be imported. This is good,
because the Python standard library has modules with lots of common names (os,
math, time, symbol, resource, etc.). Without the `__init__.py` requirement,
you would never be able to use any of those names for _any_ directory on your
Python path -- not even to store data or files unrelated to Python.

`string` is actually not the best example in this case. There is a module
called `string` but it is not so useful these days because most of its
functions are available as methods on the `str` type. But, like I mentioned,
there are lots of other modules with common names.

