title: Launching flask server from __main__.py
body: <p>I am trying to launch a flask server by running <code>python -m my_app</code></p>
  
  <p>Inside <code>my_app/__main__.py</code> I have:</p>
  
  <pre><code>from .main import app
  app.run()
  </code></pre>
  
  <p>However when running <code>python -m my_app</code> I get:</p>
  
  <pre><code> * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
   * Restarting with stat
  Traceback (most recent call last):
    File "/my_app/__main__.py", line 2, in &lt;module&gt;
      from .main import app
  SystemError: Parent module '' not loaded, cannot perform relative import
  </code></pre>
  
  <p>Indicating there is a problem with the relative import, however the strange thing is the flask app appears to try and run. Logically, next I removed the relative import with:</p>
  
  <pre><code>from my_app.main import app
  </code></pre>
  
  <p>This resulted in:</p>
  
  <pre><code> * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
   * Restarting with stat
  Traceback (most recent call last):
    File "/my_app/__main__.py", line 2, in &lt;module&gt;
      from my_app.main import app
  ImportError: No module named 'my_app'
  </code></pre>
  
  <p>Adding a bit more to <code>__main__.py</code></p>
  
  <pre><code>print('1.')
  
  from store_app.main import app
  print('2.')
  
  app.run()
  print('3.')
  </code></pre>
  
  <p>Reveals this occurs when flask reloads <code>__main__.py</code></p>
  
  <pre><code>1.
  2.
   * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
   * Restarting with stat
  1.
  Traceback (most recent call last):
    File "/my_app/__main__.py", line 3, in &lt;module&gt;
      from my_app.main import app
  ImportError: No module named 'my_app'
  </code></pre>
  
  <p>This suggests flask reloads <code>__main__.py</code> without adding the module to <code>sys.path</code>. Has anyone got a work around for this? Or is the cleanest way to manually insert the module into <code>sys.path</code></p>