title: How does _init_ file in Flask project work when starting the server?
body: <p>I'm reading the <a href="http://flask.pocoo.org/docs/1.0/tutorial/factory/" rel="nofollow noreferrer">Flask tutorial</a> and learned that in large application structure the code is moved from single <code>app.py</code> file to 'application factory function' <code>create_app</code> in <code>_init_.py</code> file, then the server can be started like this:</p>
  
  <pre><code>export FLASK_APP=flaskr
  export FLASK_ENV=development
  flask run
  </code></pre>
  
  <p>I wonder how does the last command work behind the scene, because there's no explicit call to this factory function anywhere in the code hence no app returned?</p>
  
  <p>Another <a href="http://flask.pocoo.org/docs/1.0/patterns/packages/" rel="nofollow noreferrer">Flask doc</a> tries to explain why such project structure is used but in that case code in <code>_init_.py</code> creates <code>app</code> explicitly:</p>
  
  <pre><code>from flask import Flask
  app = Flask(__name__)
  
  import yourapplication.views
  </code></pre>
  
  <p><a href="https://docs.python.org/3/tutorial/modules.html#packages" rel="nofollow noreferrer">python doc</a> says:</p>
  
  <blockquote>
    <p>The <strong>init</strong>.py files are required to make Python treat the
    directories as containing packages; this is done to prevent
    directories with a common name, such as string, from unintentionally
    hiding valid modules that occur later (deeper) on the module search
    path. In the simplest case, <strong>init</strong>.py can just be an empty file, but
    it can also execute initialization code for the package or set the
    <strong>all</strong> variable, described later.</p>
  </blockquote>
  
  <p>The last sentence mentions about 'initialization code' which can be applied in the second case(and simple project structure with <code>app.py</code> file) but I failed to apply it to the first case.</p>