You only need to add __init__.py
to directories that must be treated as packages. Any folder outside the PYTHONPATH
search path is never a package and doesn't need to have an __init__.py
.
You don't need __init__
in the top-level directory listed on PYTHONPATH
either, because that's not a package. C
is not a package here, it is a folder on module search path. You cannot import from C
; only D
is a package.
For your setup, if C:A\B\C
is on the module search path, then only items in C
can be imported. C:A\B\C\module.py
is importable as module
, and C:A\B\C\D\
is importable if there is an __init__.py
file in that directory, as import D
.
Python uses the search path to find top-level names you import; if you try to import foo
, the search path is tried one by one, each a location on your harddisk. C:\A\B\C
is one such location, it'll look inside that location, but the directory name of the location is never part of the search space.
See the Python tutorial on packages for more information.