python global parameters file
Question: I have a python script that calls multiple functions from other scripts. I
call the script giving it as a parameter a settings file with the format:

    
    
    var1 = XXX
    var2 = YYY
    ...
    

I would like to make those variables global so they can be used by every
function I call, but without having to pass them explicitely. What is the best
way to do this?

Thank you.


Answer: The simple way is using a settings (e.g. `settings.py`) module as suggested.

You simply define the variables in the module and they will be available to
any module that imports the `settings` module via `settings.var_name`.

Example:

file `settings.py`:

    
    
    var_name = "value"
    

file `m.py`:

    
    
    #!/usr/bin/env python
    import settings
    print settings.var_name
    

A preferred way is using the environment (child process inherits the
environment) to pass it in a global context. You can use
[os.environ](https://docs.python.org/2/library/os.html#process-parameters) for
this purpose.

An example of how to do it:

file `a.py`:

    
    
    #!/usr/bin/env python
    import os
    import b
    os.environ['var_name'] = 'value'
    b.echo()
    

file `b.py`:

    
    
    import os
    
    def echo():
        print os.environ['var_name']
    

