question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

config option for "flask run" (e.g. --dotenv/-e)

See original GitHub issue

Would 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:closed
  • Created 5 years ago
  • Comments:9 (6 by maintainers)

github_iconTop GitHub Comments

6reactions
davidismcommented, Mar 1, 2019

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 with FLASK_APP and FLASK_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 a cli.py file, and register your app with it instead of loading from FLASK_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 a project command instead of the flask command.

1reaction
davidismcommented, Jun 15, 2022

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Automatically Load Environment Variables in Flask
Learn how to use python dotenv to automatically load environment variables into your Flask projects.
Read more >
Configuration Handling — Flask Documentation (2.2.x)
When using flask run to start the development server, an interactive debugger will be shown for unhandled exceptions, and the server will be...
Read more >
Flask: app.config settings from .env &. flaskenv in mod_wsgi
I am using apache2, mod_wsgi, Flask, Python 3.6 and SQLAlchemy. My backend works fine locally on my Mac using pure Flask. Having python-dotenv...
Read more >
Configuring Your Flask App - Hackers and Slackers
The simplest way to configure a Flask app is by setting configuration variables directly in a config file such as config.py. from flask...
Read more >
Flask Configuration From Environment Variables - YouTube
In this video I show you how to use your operating system's environment variables to configure your Flask app. I'll show you how...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found