title: How to configure __main__.py, __init__.py, and setup.py for a basic package setup?
body: <h2>Background:</h2>
  
  <p>I have a directory structure like so: </p>
  
  <pre><code>Package/
      setup.py
      src/
          __init__.py
          __main__.py 
          code.py
  </code></pre>
  
  <p>I want to be able to run the code in a lot of different ways. </p>
  
  <ol>
  <li><p><code>pip install Package</code> and then <code>python</code> and then <code>from Package import *</code></p></li>
  <li><p><code>python -m Package</code> which should do the thing in <code>__main__.py</code></p></li>
  <li><p><code>python __main__.py</code> which should also do the thing in <code>__main__.py</code> but this time, we assume you've downloaded source rather than <code>pip installing</code>. </p></li>
  </ol>
  
  <p>Now I've gotten the first two to work, but with a messy setup: </p>
  
  <p><strong>setup.py:</strong> </p>
  
  <pre><code>setup(
      name='Package',
      packages=['Package'],
      package_dir={'Package': 'src'},
      ...
      entry_points={ 'console_scripts': ['Package = src.__main__:main' ] }
  </code></pre>
  
  <p><strong>__init__.py:</strong></p>
  
  <pre><code>from Package.code import .......
  </code></pre>
  
  <p><strong>__main__.py:</strong></p>
  
  <pre><code>from . import .......
  </code></pre>
  
  <p>What would make more sense to me would be in both cases to write</p>
  
  <pre><code>from code import ........
  </code></pre>
  
  <p>but that gives me import errors. </p>
  
  <h1>Question:</h1>
  
  <p>Is the way I have it really the only way? </p>
  
  <p>And most importantly, how do I support the third use case? Right now, <code>python __main__.py</code> throws</p>
  
  <pre><code>File "__main__.py", line 10, in &lt;module&gt;
      from . import code
  ImportError: cannot import name 'class defined in code.py'
  </code></pre>
  
  <h2>Notes:</h2>
  
  <p>I have read </p>
  
  <ul>
  <li><a href="https://chriswarrick.com/blog/2014/09/15/python-apps-the-right-way-entry_points-and-scripts/" rel="noreferrer">https://chriswarrick.com/blog/2014/09/15/python-apps-the-right-way-entry_points-and-scripts/</a></li>
  <li><a href="http://setuptools.readthedocs.io/en/latest/setuptools.html" rel="noreferrer">http://setuptools.readthedocs.io/en/latest/setuptools.html</a></li>
  <li>The many questions here that look like this one but don't answer my question above.</li>
  </ul>