title: ModuleNotFoundError when running imported Flask app
body: <p>I have a python module with the following layout:</p>
  
  <pre><code> foo
    |  __init__.py
    |  __main__.py
    |  bar.py
  </code></pre>
  
  <p><code>__init__.py</code> is empty.</p>
  
  <p>Content of <code>foo/bar.py</code>:</p>
  
  <pre><code>from flask import Flask
  app = Flask(__name__)
  def baz(): pass
  </code></pre>
  
  <p>When running <code>python3 -m foo</code> i get confusing results.</p>
  
  <p>Contents of <code>foo/__main__.py</code></p>
  
  <pre><code># Results in a ModuleNotFoundError: No module named 'foo'
  from foo.bar import app
  app.run()
  
  # Raises no error and correctly prints the type
  from foo.bar import app
  print(type(app))
  
  # Also runs without an error
  from foo.bar import baz
  baz()
  </code></pre>
  
  <p>Why is it possible to import and execute a function from this module, but when trying to do the same with a flask app it results in a <code>ModuleNotFoundError</code>?
  I just can't see any way this makes any sense.</p>
  
  <p>Edit:</p>
  
  <p>The error is persistent even with this code:</p>
  
  <pre><code>from foo.bar import app
  
  print(type(app))
  app.run()
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>&lt;class 'flask.app.Flask'&gt;
   * Serving Flask app "foo.bar" (lazy loading)
   * Environment: production
     WARNING: Do not use the development server in a production environment.
     Use a production WSGI server instead.
   * Debug mode: on
   * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
   * Restarting with stat
  Traceback (most recent call last):
    File "/home/user/projects/ftest/foo/__main__.py", line 1, in &lt;module&gt;
      from foo.bar import app
  ModuleNotFoundError: No module named 'foo'
  </code></pre>
  
  <p>So, obviously the module <strong>can</strong> be imported, because <code>type(app)</code> works just fine and flask <strong>does start</strong>. It seems like flask does a reload and is messing around with imports somehow.</p>
  
  <p>Edit 2:</p>
  
  <p>I turned debug mode off and it works just fine.
  This error only occurs if you set <code>export FLASK_DEBUG=True</code> or explicitly enable debug via <code>app.config["DEBUG"] = True</code></p>