How to run Flask app by flask run or custom command without set FLASK_APP manually
Create a file named .flaskenv
under the root of your project, put this content into it:
FLASK_APP=fsksc.server.app:create_fsksc_app
then install python-dotenv:
$ pip install python-dotenv
Now the env var should be set automatically when you excute your run command.
Ok, so Grey's answer put me on the right track..
Unfortunately, because of how flask uses the .flaskenv
files, paths have to either be absolute, or relative to the current working directory of the user. Neither of these approaches will work for other users installing the package.
However, when playing with this I found that doing a simple os.environ['FLASK_APP']
works! It just has to be very early on in the script execution. Then the path can be set dynamically based on the location of the installed script, and I think everything seems to work:
flaskenv_app_path = os.path.join(os.path.dirname(os.path.dirname(__file__)), 'fsksc', 'server', 'app.py')flaskenv_app_func = '{}:create_fsksc_app'.format(flaskenv_app_path)os.environ['FLASK_APP'] = flaskenv_app_func
I've updated the minimal example code with this here: https://github.com/ewels/flask-subcommand-issue
Thank you for your help Grey Li!