Comments (6)
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?
from django-environ.
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.
from django-environ.
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.
from django-environ.
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.
from django-environ.
@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.
from django-environ.
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.
from django-environ.
Related Issues (20)
- Environment variables' reimports not supported HOT 1
- Officially support Django 4.2 and Python 3.12 on PyPI HOT 1
- Ask for next release HOT 1
- v0.11.0 Introduces parsing error for strings containing more than one $ symbol HOT 6
- v0.11.1: $-symbol issue HOT 5
- v0.11.0/ v0.11.1 breaks on Mac OSX HOT 1
- UserWarning: Engine not recognized from url : sqlite3 HOT 3
- Path issues with read_env since 11.0+ HOT 11
- Simplicity and Stability vs Advanced Features HOT 2
- Hash sign inside double quotes is interpreted as the start of a comment HOT 5
- Deprecated Code from python 2.x to 3.x HOT 1
- db_url() fail with oracle dsn
- db support for CockroachDB HOT 1
- Wrong missing environment variable name if using prefix HOT 1
- Django's RedisCache OPTIONS needs to be lowercase
- Double quotes no longer work for including the `#` in a string HOT 5
- ModuleNotFoundError: No module named 'environ'
- Support new db option `pool` in Django 5.1
- DeprecationWarning: 'pkgutil.find_loader' is deprecated and slated for removal in Python 3.14; use importlib.util.find_spec() instead on 3.12 HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from django-environ.