1 year ago

#362187

test-img

RVA92

Best Pratice Setting global, changeable parameters in Python package

I have made a python package with a basic structure like this (this is only the code structure).

mypackage
    mypackage
        __init__.py
        module1.py
        module2.py

In the init file I import functionalities from module1 and module2. The problem: I would like to make a package-parameter handler, such that you can change general behavior of the package by simply changing this parameter. Example:

import mypackage
mypackage.settings['show_progressbar'] = False

In this example settings is a dictionary. For inspiration I have considered matplotlib's rcparams. However, I do not know how to implement this. Placing the settings dict in the init file while also using it in module1 and module2 will yield circular import.

init.py

from mypackage.module1 import function1
from mypackage.module2 import function2
settings = {'show_progressbar': True}

module1.py

from mypackage import settings

def function1(show_progressbar=settings['show_progressbar']):
     ...some code with a progressbar ...

So how do I make an editable settings dictionary (or other object) that may be used to control behavior of an instance of the module?

python

package

settings

0 Answers

Your Answer

Accepted video resources