title: Why do I need __init__.py at every level?
body: <p>Given that I have the following directory structure with <code>.</code> being the current working directory</p>
  
  <pre><code>.
  \---foo
      \---bar
          \---__init__.py
          \---baz.py
  </code></pre>
  
  <p>When I run <code>python -c "import foo.bar.baz"</code> I get</p>
  
  <pre><code>Traceback (most recent call last):
    File "&lt;string&gt;", line 1
  ImportError: No module named foo.bar.baz
  </code></pre>
  
  <p>If I <code>echo "" &gt; foo/__init__.py</code>, the above command works.</p>
  
  <p>Am I doing something wrong or do I  misunderstand the point of <code>__init__.py</code>?  I thought it was to stop modules existing where they shouldn't, e.g. a directory named <code>string</code>, but if you replace <code>foo</code> with <code>string</code> in my example, I'm seemingly forced to create the module that should never be used, just so I can reference a file deeper in the hierarchy.</p>
  
  <p><strong>Update</strong></p>
  
  <p>I'm working with a build system that's generating the <code>__init__.py</code>'s for me and is enforcing the directory structure and while I could mess with the hierarchy, I'd prefer to just add the <code>__init__.py</code> myself. To change the question slightly, why do I need a python package at every level instead of just at the top? Is it just a rule that you can only import modules from the python path or from a chain of packages off of the python path?</p>