Comments (7)
I'd like to make a design suggestion: avoid configuration handling in libraries. The "right place" to handle configuration is your project (that requires/uses the library).
It's better make your library configuration-agnostic, so, the users of your library can use any configuration system. And you can also reduce the number of depedencies.
"Wrong" way
# mylib.py
class Client:
def __init__(self, url=None):
self.url = config("URL", default="http://localhost/")
# myproject.py
from mylib import Client
client = Client()
"Right" way
# mylib.py
class Client:
def __init__(self, url):
self.url = url
# myproject.py
from mylib import Client
url = config("URL", "http://localhost")
client = Client(url)
from python-decouple.
I faced this issue when trying to use decouple.config
in a jupyter notebook.
This PR #28 allows the users to instantiate AutoConfig
with a custom initial search path. For example, in a jupyter notebook:
config = AutoConfig(os.getcwd())
from python-decouple.
When you from decouple import config
you're using the default AutoConfig instance.
It has a magic that searches for the settings.ini file from the caller's module directory. If it can't find there, it keeps searching up the tree.
If you want to have full controle and point directly where your settings.ini is:
from decouple import RepositoryIni, Config
config = Config(RepositoryIni('path/to/settings.ini'))
DEBUG = config('DEBUG')
This API has room for improvement. You're the 2nd to request something like this.
from python-decouple.
I have interest in this feature too. I solve it by override _caller_path
from decouple import AutoConfig
def _caller_path(self):
return os.getcwd()
AutoConfig._caller_path = _caller_path
config = AutoConfig()
from python-decouple.
@nielsonsantana can you provide a patch so we can invert the dependency on the path?
from python-decouple.
I trying figure out how change your code. Are you suggesting to override "_caller_path"?
Replace:
def _caller_path(self):
# MAGIC! Get the caller's module path.
frame = sys._getframe()
path = os.path.dirname(frame.f_back.f_back.f_code.co_filename)
return path
for
def _caller_path(self):
return os.getcwd()
In my tests, it's work based on where it is called from. Per example, if it is called from a terminal, it will use the local .env
based on the path of the terminal.
from python-decouple.
Fixed tks to @rmax
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.