title: How to run Flask app by flask run or custom command without set FLASK_APP manually
body: <h2>Use case</h2>
  
  <p>I have a python package using a <code>click</code> group to have multiple command line subcommands.
  In addition to this, I would like to have a small flask application.</p>
  
  <p>The other subcommands are the primary function of the package - <em>not</em> the flask application.
  As such, I would like the flask commands to be nested under their own group.</p>
  
  <h2>Example</h2>
  
  <p>I made a minimal example to demonstrate my problem - it's on GitHub here: <a href="https://github.com/ewels/flask-subcommand-issue" rel="nofollow noreferrer">https://github.com/ewels/flask-subcommand-issue</a></p>
  
  <h3>What works</h3>
  
  <p>In the minimal example, I set up a mini flask installation that runs with the <code>fsksc_server</code> command.
  This is thanks to a setuptools <code>console_scripts</code> entry point hook in <code>setup.py</code>.</p>
  
  <p>The command works perfectly, exactly as you would expect:</p>
  
  <pre><code>$ fsksc_server --help
  
  Usage: fsksc_server [OPTIONS] COMMAND [ARGS]...
  
    Run the fsksc server flask app
  
  Options:
    --version  Show the flask version
    --help     Show this message and exit.
  
  Commands:
    routes  Show the routes for the app.
    run     Runs a development server.
    shell   Runs a shell in the app context.
  </code></pre>
  
  <pre><code>$ fsksc_server run
  
   * Environment: production
     WARNING: Do not use the development server in a production environment.
     Use a production WSGI server instead.
   * Debug mode: off
   * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
  </code></pre>
  
  <p><em>(I haven't set up any routes, so visiting the URL throws a 404, but the server is running fine..)</em></p>
  
  <p>To get the flask commands in a click subcommand I have used flask <code>add_command</code> with the flask group.
  This main command is <code>fsksc</code>. The flask subcommand should be <code>shell</code>.
  The intention is to make <code>fsksc shell run</code> launch the development server.</p>
  
  <p>The commands show up properly, so this part seems to work:</p>
  
  <pre><code>$ fsksc --help
  
  Usage: fsksc [OPTIONS] COMMAND [ARGS]...
  
  Options:
    --help  Show this message and exit.
  
  Commands:
    cmd1
    cmd2
    server  Run the fsksc server flask app
  </code></pre>
  
  <h3>What doesn't work</h3>
  
  <p>When doing anything under the <code>server</code> subcommand, I get a warning message about a <code>NoAppException</code> exception:</p>
  
  <pre><code>$ fsksc server --help
  
  Traceback (most recent call last):
    File "/Users/ewels/miniconda2/envs/work/lib/python2.7/site-packages/Flask-1.0.2-py2.7.egg/flask/cli.py", line 529, in list_commands
      rv.update(info.load_app().cli.list_commands(ctx))
    File "/Users/ewels/miniconda2/envs/work/lib/python2.7/site-packages/Flask-1.0.2-py2.7.egg/flask/cli.py", line 384, in load_app
      'Could not locate a Flask application. You did not provide '
  NoAppException: Could not locate a Flask application. You did not provide the "FLASK_APP" environment variable, and a "wsgi.py" or "app.py" module was not found in the current directory.
  Usage: fsksc server [OPTIONS] COMMAND [ARGS]...
  
    Run the fsksc server flask app
  
  Options:
    --version  Show the flask version
    --help     Show this message and exit.
  
  Commands:
    routes  Show the routes for the app.
    run     Runs a development server.
    shell   Runs a shell in the app context.
  </code></pre>
  
  <p>Trying to run the server gives a similar error:</p>
  
  <pre><code>$ fsksc server run
  
   * Environment: production
     WARNING: Do not use the development server in a production environment.
     Use a production WSGI server instead.
   * Debug mode: off
  Usage: fsksc server run [OPTIONS]
  
  Error: Could not locate a Flask application. You did not provide the "FLASK_APP" environment variable, and a "wsgi.py" or "app.py" module was not found in the current directory.
  </code></pre>
  
  <h3>Crappy workaround</h3>
  
  <p>I can fix this by defining the <code>FLASK_APP</code> environment variable correctly.
  Then <code>flask run</code> works as expected:</p>
  
  <pre><code>$ export FLASK_APP=/Users/ewels/GitHub/flask-subcommand-issue/fsksc/server/app.py:create_fsksc_app
  
  $ fsksc server run
  
   * Serving Flask app "/Users/ewels/GitHub/flask-subcommand-issue/fsksc/server/app.py:create_fsksc_app"
   * Environment: production
     WARNING: Do not use the development server in a production environment.
     Use a production WSGI server instead.
   * Debug mode: off
   * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
  </code></pre>
  
  <p><code>flask run</code> also works.</p>
  
  <p><em>But</em> - I don't want to have to get my users to do this!
  I also don't want to pollute my main command group with the flask subcommands
  (in reality I have a lot more subcommands in the main group).</p>
  
  <h3>The question</h3>
  
  <p>What do I need to do to make this work as I intend, without having to define <code>FLASK_APP</code> and as a nested group in click?</p>
  
  <p>Thank you in advance for any help!</p>