Comments (8)
All variables are required by decouple. If you have a condition, you could use plain python instead of changing decouple's api.
Can't you achieve what you want by:
DEBUG = config('DEBUG', cast=bool)
if not DEBUG:
THIS_IS_REQUIRED = config('THIS_IS_REQUIRED', default='local-value')
THIS_IS_NOT_REQUIRED = config('THIS_IS_NOT_REQUIRED', default='local-value')
If you must have it declared and can't use the if
, you should consider using the default
parameter to None
. This will avoid the UndefinedValueError
.
from python-decouple.
Sorry, this is my fault, let me try explaining it again. A more precise sentence that describes my problem would be:
I'd like to be able to tell that some env variables must be set in a production environment.
(be it actually a environment variable
, in an .env file
or in an .ini file
)
Therefore, a more precise implementation of the behavior I'm trying to achieve using the current API would be:
DEBUG = config('DEBUG', cast=bool)
if not DEBUG:
THIS_IS_ANY_PYTHON_VAR = config('THIS_IS_REQUIRED_ONLY_IN_PRODUCTION')
else:
THIS_IS_ANY_PYTHON_VAR = config(
'THIS_IS_REQUIRED_ONLY_IN_PRODUCTION', default='local-value')
THIS_OTHER_PYTHON_VAR = config('THIS_IS_NEVER_REQUIRED', default='local-value')
Note that if DEBUG
is False
and the environment variable THIS_IS_REQUIRED_ONLY_IN_PRODUCTION
is not set, it will raise an exception. If DEBUG
is True
, it will never raise an exception.
from python-decouple.
Hi @filipeximenes,
An alternative is to use decouple's undefined
object, as in this example:
from decouple import config, undefined
DEBUG = config('DEBUG', cast=bool, default=False)
DB_NAME = config('DB_NAME', default='default-db-name' if DEBUG else undefined)
Best regards,
from python-decouple.
Cool, this fulfills my needs! Will close the issue.
Thanks!
from python-decouple.
Pretty cool, Fernando!!!
However, thinking about the project design, somehow I think Xima is
breaking the premisse that the settings should be production oriented and
developers should improve their environments to make their development
instances behave as they need.
Personally, I see the need of hardcoding test values as an anti-pattern.
from python-decouple.
@henriquebastos I put the word test
under my example, not @filipeximenes. I've updated to default-db-name
.
I agree with you that settings should be production oriented, but some times I use decouple
as "a reasonable default value that I can use into development and can be changed on production without changing the code".
As Tim Peters said:
Special cases aren't special enough to break the rules.
Although practicality beats purity.
from python-decouple.
I have a situation where I have access keys to external services that are required for some actions in the system that not all developers will need to deal with. I don't want developers that are not working in this part of the system to bother with this keys, but at the same time I want to make sure my production environment has the correct configuration and screams if there's something missing.
I think the same applies when we set databases like this:
DATABASES = {
'default': config(
'DATABASE_URL',
default='sqlite:///' + BASE_DIR.child('db.sqlite3'),
cast=db_url
)
}
There is a risk to end up with a sqlite database in production if someone forgets to set the DATABASE_URL
environment variable.
from python-decouple.
I understand and share this need.
I just rather create a contrib shellscript to launch process with
dummy/dev config values instead of complicating the use of decouple and
risking a misbehavior in production. This can safely be added to the
repository under /contrib
.
My rationale is: if it's a dev need, let's find a dev related solution
instead injecting ambiguity (in behavior) to the settings.py
Many options! Pick what suits you.
from python-decouple.
Related Issues (20)
- Pycharm and the python-decouple package HOT 2
- Support for multiline strings HOT 9
- Pyright reportGeneralTypeIssues error with cast HOT 1
- Add type hints HOT 1
- Feature request: Cascaded settings files HOT 8
- question Changing settings HOT 2
- Feature Request: Fallback to .env.example HOT 3
- decouple.UndefinedValueError: SECRET_KEY not found. Declare it as envvar or define a default value. HOT 4
- Csv(None) hangs
- Infinite recursion in _find_file on Windows when called via pytest HOT 5
- Cascaded Settings Files
- This bugs me so much - Install name differs from package name causing IDE warnings HOT 1
- Feature idea: optional value HOT 2
- decouple.UndefinedValueError: SECRET_KEY not found. HOT 1
- ENV when use Csv can't be optional HOT 1
- Issue with Environment Variables on AWS Elastic Beanstalk HOT 5
- Issue with typings using Pylance type checking HOT 2
- profiling through environment variables HOT 1
- Issues with installation using pipenv HOT 2
- Installation with Poetry give back an error 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 python-decouple.