-
-
Notifications
You must be signed in to change notification settings - Fork 16.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Discussion: from_envvar's behavior #2368
Comments
IMO a config file does not belong the package directory, but your suggestion sounds a lot like you want the environment variable to specify an import name (such as Let's say I install my app in I know there are some example projects out there that have |
It's a common pattern in Django, and even in the Flask community. Example: https://github.com/rtfd/readthedocs.org/tree/master/readthedocs/settings A check of github code search shows a decent share of Flask, >50%, use
A popular flask example, flask-overholt using a config in its own package and applying it in a factory. I could probably give a lot more to support that module strings are commonplace. I'm not saying that it's righteous or technically superior, but it's a de facto way of loading configs, especially in Flask.
I'm trying to make sure I'm understanding that correctly. Flask supports importing from objects. There's also examples in official documentation. In addition, what if the configuration deliberately wants to use site-packages? Especially when they deploy? For instance, dj-database-url is imported in django settings. (I'm assuming they're using project's site-packages and not their system's.) Even in situations where configurations are not stored in site-packages, people try to do add the directory to site-packages manually anyway: Here are a few cases of uwsgi scripts adding directories for settings by hand via |
Let's assume you're right. Keeping configs with special/sensitive stuff (like db credentials) there is a bad idea. Flask has already been around and supported configs that are in classes. These can't be invoked via Keep in mind, this discussion is not about removing
That makes sense.
Let's assume that. It doesn't rule out having settings added to site packages from outside the project's code being imported via a string ( |
I'm happy with the The next version will also allow |
There is an issue I've been having for a few years regarding Flask and I failed to articulate it correctly. I find
from_envvar
's behavior unintuitive. Also, I haven't been able to find prior discussions mentioning it. Probably because of Chesterton's fence, or they feel they'd be told to work around it. Which only takes one line:app.config.from_object(os.environ["app.config.dev"])
, see Google results 😄The first google result I see is people perplexed by from_envvar. Why? I'm not sure. For some reason, in recent years, Django (and frankly, Flask also) got devs accustomed to importing modules via strings.
Current behavior:
from_envvar
forwards tofrom_pyfile
, which strictly requires an absolute/relative file path (i.e. not a module string).Expected behavior:
from_envvar
should wrapfrom_object
Why:
from_envvar
only works with filesFile from
from_envvar
are interpreted as modules anywayfrom_envvar
(from_pyfile
) cannot point to classesfrom_object
supports string module/class paths (e.g.app.config.dev
), which works with environmental variablesfrom_object
supports both classes and modules. Especially important since the official documentation gives examples of using classes.It's more customary for users invoking configuration by files to do so via CLI arguments.
A local, dev, staging, or production deployment in practice would opt to use environmental variables to point to a python object (module/class) via string.
For instance: Let's say we're deploying to production with a uwsgi config. Since python production deployment is highly driven by packages, we've likely already done the work of figuring out our python environmental / package locations. It'd be more intuitive to point to a python object by string, rather than by file path.
Alternative:
from_envvar
should additionally wrapfrom_object
for non-file paths.The text was updated successfully, but these errors were encountered: