0

I'm trying to read configurations from a property file and store those properties in a variable so that it can be accessed from any other class.

I'm able to read the configuration from the config file and print the same but I'm getting an exception when those variables are accessed from some other class.

my config file

Config.cfg.txt
    [Ysl_Leader]
    YSL_LEADER=192

Generic class where i will store my properties in a variable. ConfigReader.py

    import configparser

        class DockerDetails:
            config = configparser.RawConfigParser()
            _SECTION = 'Ysl_Leader'
            config.read('Config.cfg.txt')
            YSL_Leader = config.get('Ysl_Leader', 'YSL_LEADER')
            print(YSL_Leader)

Another class where I'm trying to get the get the 'YSL_Leader' value

def logger(request):
    print(ConfigReader.DockerDetails.YSL_Leader)

Exception:

  File "C:\Users\pvivek\AppData\Local\Programs\Python\Python37-32\lib\configparser.py", line 780, in get
    d = self._unify_values(section, vars)
  File "C:\Users\pvivek\AppData\Local\Programs\Python\Python37-32\lib\configparser.py", line 1146, in _unify_values
    raise NoSectionError(section) from None
configparser.NoSectionError: No section: 'Ysl_Leader'

FYI: I'm not getting any exception when I run ConfigReader.py alone

vivek.p.n manu
  • 286
  • 1
  • 4
  • 19
  • Perhaps your code is not looking in the right directory for the Config.cfg.txt file. `config.read()` will _not_ raise an exception if the file can't be opened. – John Gordon Jun 24 '19 at 15:47
  • but when I run ConfigReader.py directly, I'm getting proper value from Config.cfg.txt and im facing issue only when I try to get that value in some other class and I'm using ConfigReader.DockerDetails.YSL_Leader to fetch that variable. Please let me know if my question is not properly formed – vivek.p.n manu Jun 24 '19 at 15:51
  • 1
    Before `config.read()`, print the result of `os.getcwd()` to see if you're in the right directory. – John Gordon Jun 24 '19 at 17:23
  • Thanks !!! it worked at last. The issue was when I printed os.getcwd() in ConfigReader.py, it was pointing to the proper location where the file is present but when I'm calling from another calss it was not pointing to the proper sub folder – vivek.p.n manu Jun 25 '19 at 04:23

1 Answers1

0

analyzing your question you try to create an environment file, if it is the case because you are using a class to read the file, you must perform this operation in its constructor (remember to make the reference self) and instantiate to be able to access its values, You can perfectly use a function to perform this reading, remembering that to access the result can be treated as a dictionary

configuration file name = (config.ini)

[DEFAULT]
ANY = ANY

[Ysl_Leader]
YSL_LEADER = 192

[OTHER]
VALUE = value_config
# using classes
class Cenv(object):
    """
    [use the constructor to start reading the file]
    """
    def __init__(self):
        self.config = configparser.ConfigParser()
        self.config.read('config.ini')

# using functions
def Fenv():
    config = configparser.ConfigParser()
    config.read('config.ini')
    return config

def main():
    # to be able to access it is necessary to instantiate the class
    instance = Cenv()
    cfg = instance.config
    # access the elements using the key (like dictionaries) 
    print(cfg['Ysl_Leader']['YSL_LEADER'])
    print(cfg['OTHER']['VALUE'])

    # call function and assign returned values
    cfg = Fenv()
    print(cfg['Ysl_Leader']['YSL_LEADER'])
    print(cfg['OTHER']['VALUE'])
    # in the case of django assign values ​​in module settings

if __name__ == '__main__':
    main()

you can interpret the result as follows (dictionary)

{
    "Ysl_Leader": {
        "YSL_LEADER": "192"
    },
    "OTHER": {
        "VALUE": "value_config"
    }
}
yson
  • 276
  • 1
  • 6
  • Thanks for your time buddy... I tried it and it works perfectly fine but my use case is to write a generic class where I can store properties in a variable and access the same from all other class – vivek.p.n manu Jun 25 '19 at 04:26