Search code examples
pythonvariablesstoreargparseflags

How to save global variables in an extern file in Python?


I'm looking for a clean way to use global variables in many files. So I want to write a parameters.py file where all my parameters will be stored.

How can i achieve this ? I've heard about FLAGS or argparse but i don't understand how to use them. Can someone explain me how to build this logic and then how to use it to import a variable.

I want to use the standards of Tensorflow if possible ...


Solution

  • You can simply build a settings.py module where all variables defined in it are global:

    var1 = 'value1'
    var2 = 'value2'
    ...
    

    And then in the other scripts or modules of the same project, you can simply refer to these global variables with:

    import settings
    print(settings.var1)
    print(settings.var2)
    ...