Skip to content
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

Restore defaults in .env #523

Open
dacotagh opened this issue Apr 18, 2024 · 6 comments
Open

Restore defaults in .env #523

dacotagh opened this issue Apr 18, 2024 · 6 comments

Comments

@dacotagh
Copy link

When we add some values into .env file, there is no way to remove those values until kill/start server (reload/restart doesn't help).

Is it any way to restore default values?

If no, maybe it could be implemented as special syntax, for example with minus before name?

settings.py:

KEY = env.int('KEY', 0)

.env:

KEY=3

this doesn't work, KEY is still 3:

#KEY=3

Suggestion for .env (should make KEY 0):

-KEY=3
@girapa
Copy link

girapa commented Apr 22, 2024

Not sure I've understood clearly your issue, but I was running into "cached" .env variables myself and seems like there's a an override="False" set by default in read_env. Set it to True and it will override previously cached/saved environment variables.

Is that the issue you were encountering?

@dacotagh
Copy link
Author

Can't see override option of read_env. Do you mean overwrite?
No, it doesn't related to the problem.

Will try explain scenario:

I have settings.py with

env = environ.Env()
environ.Env.read_env('.env', overwrite = True)
KEY = env.int('KEY', 0)

.env file with

KEY=3

Now it is ok, KEY is equal to 3.

If I want to change KEY to e.g. 5, I will change it in .env file

KEY=5

Development django reloads (I've added watchdog on .env file).
Production version restared using killall -HUP uwsgi
And KEY becames 5, ok.

Now I need to reset KEY to initial value, and I try to change .env file:

#KEY=5

It is reloaded/restarted again.
But value doesn't changed to 0, it remains 5, because of values are in env variables.

Of course, I can kill/start server.
But it is not so suitable and can be long enough.

Of course, I can set KEY to initial value.
But it is not so comfortable if KEY have something not so trivial as initial value.

So I suppose to add special sign to use default value from settings.py

-KEY=5

will lead to 0.

@girapa
Copy link

girapa commented Apr 23, 2024

I see your use case now.
In that, from the docs I can see support for complex values, one of which is the ability to pass a dict. In your case, your best bet would be to pass a dict with your two values and then create some logic to set the value in your application.

@dacotagh
Copy link
Author

No, I think you don't catch.

I describe very common scenario that any developer encounter with while development stage with any values in .env file.
And none of specific action should be done with it.

Well, if it hard to understand, I will do a patch.

@pataquets
Copy link

@dacotagh Could you provide a real-world example why and where is this needed?
Look like to me that you're trying to solve the problem the wrong way. I mean, common practice (The Twelve-Factor App) is to redeploy every time a env var configuration change is done.

Case in point from Cloud Run revisions docs:

When you deploy to a service or change the configuration of a service, an immutable revision is created.

If the setting you're changing it's meant to be configured more often, you might be interested in some other configuration method (e.g. database-backed), such as Constance.

@dacotagh
Copy link
Author

dacotagh commented May 1, 2024

No, you're talking about dynamic settings, but I'm talking about static settings and it's changes for testing purpose.

We have ordinal configuration: django application, development config (runserver + sqlite), production config (uwsgi + postgres).
three config files: settings.py, settings_dev.py, settings_pro.py with hundreds of variables.
Most of them are wrapped with environ.
We have several .env files for different instances.
Everything is ok.

Now I need to change some of these settings for testing purpose (it can be development configuration or production as well).

For example, we have SESSION_SECURITY_EXPIRE_AFTER variable.
It set as

SESSION_SECURITY_EXPIRE_AFTER=env.int('SESSION_SECURITY_WARN_AFTER', 356 * 24 * 60 * 60) # one year

for development configuration and

SESSION_SECURITY_EXPIRE_AFTER=env.int('SESSION_SECURITY_EXPIRE_AFTER', 8 * 60 * 60) # 8 hours

for production configuration

I want to change it to 30 seconds to test, so I add to .env file line

SESSION_SECURITY_EXPIRE_AFTER=30

django reloads application just like it does for any changes in python code.

After successful testing I want to change it back.
But neither removing this line from .env file, commenting or somewhat else do not work, because it remains 30.

So we have some strange situation - for any of changes or settings in python code I can change it forward and back easily, but for values in .env I can't do the same, because it persists in environ.

Before using Environ we used 'import local_settings.py' pattern and we haven't such problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants