config option for "flask run" (e.g. --dotenv/-e)
See original GitHub issueWould it make sense to allow passing a --config/-c
option to flask run
, that would then be exposed to users somehow for subsequent passing to e.g. app.config.from_pyfile()
?
(I’m imagining this done in a reusable way so that a “flask gunicorn” custom command (the likes of which I’ve already implemented) could accept and expose a -c
value similarly, so that the app could be configured from this same provided path whether running in development or production.)
This would reduce the dependence on an environment variable to specify this. Reason I ask is that many of the Flask users I’m supporting are allergic to environment variables for the reasons mentioned in #3095, and have been missing some way to pass a CLI option to flask run
to help configure their apps. Thanks 😊
Issue Analytics
- State:
- Created 5 years ago
- Comments:9 (6 by maintainers)
I think we might have to disagree about env vars to some degree. We did add support for python-dotenv to read a
.flaskenv
file so that you don’t have to export the vars in your terminal, maybe that mitigates some of your concerns.The issue I see with adding a config option is that it now dictates one way to configure the app that’s not portable to how you’d be deploying to production. If users use this and then use mod_wsgi, for example, they’d have to read the config themselves anyway. It lends preference to
from_pyfile
as opposed to other functions in Flask or patterns in tutorials. It wouldn’t solve the issue withFLASK_APP
andFLASK_ENV
, which aren’t set from config.I think you might be looking for a custom run command that’s more appropriate to your (and probably others) use cases. That might be a good fit for an extension that you’d then install for each of your projects. The docs on Flask’s CLI document how extensions can register commands automatically. For example, you could register a
run
command that made assumptions about where the app is, and what its env and config are.You could also create your own
FlaskGroup
in acli.py
file, and register your app with it instead of loading fromFLASK_APP
. You could add options and extra processing to this cli group. And if you’re using setuptools, you can add an entry point to this as aproject
command instead of theflask
command.I’ve been doing some work on enabling
--app
and--env
as options (yes, it’s happening for real this time, I actually figured it out). However,--env-file
is trickier. The file has to get loaded very early so that the environment is set before the processing pipeline runs. But since the pipeline hasn’t run yet, the option hasn’t been parsed, so it’s not possible to get the file yet.It could be made an
eager
option, then it could load the env when processing, before any other options are processed. However, I’m not sure if there would be corner cases to this.