Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The right way to share settings among modules

Tags:

python

I have a project with 10 different python files. It has classes and functions - pretty much the lot. I do want to share specific data that will represent the settings in the project between all the project files.

I came up with creating a settings.py file:

settings = {}
settings['max_bitrate'] = 160000
settings['dl_dir'] = r"C:\Downloads"

and then I import the class from every file.

Is there a more suitable way to do it?

like image 395
iTayb Avatar asked Oct 23 '25 02:10

iTayb


2 Answers

I'm probably a little old-school in this regard, but in my latest project, I created a config file in /etc, then created a config module that uses ConfigParser to read it in and make it available, and import that config module wherever I need to read settings.

like image 54
zigg Avatar answered Oct 24 '25 16:10

zigg


Your method sounds good to me, and has the advantage that you can easily change the implementation of the settings module, for example to use configuration files or the windows registry, or to provided a read only API.

like image 25
aquavitae Avatar answered Oct 24 '25 14:10

aquavitae



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!