Search code examples
pythonpython-3.xyamlpyyaml

ScannerError: mapping values are not allowed here in "config.yaml"?


I want to configure a yaml file for all project configuration, but this below file I am not able to read/parse it?

Here is the error I am getting, What am I doing wrong here?

ScannerError: mapping values are not allowed here
  in "config.yaml", line 7, column 13

appName: test
logLevel: WARN


TESTER:
    ENVIRONMENT: staging
      CONFIG:
        DATABASE:
          HOST: 2123.3123.2112.12
          USERNAME: x
          PASSWORD: y
          DB: Q
        CLASSIFIER:
          IMG_WIDTH: 380
          IMG_HEIGHT: 380
          HOST: 0.0.0.0:3201

Solution

  • The Issue is with the indentation of your yaml. ENVIRONMENT is having different indent compared to CONFIG, which is in next line.

    You can use any online validators like http://www.yamllint.com or https://codebeautify.org/yaml-validator to validate your yaml files.

    This is valid YAML:

    appName: test
    logLevel: WARN
    
    
    TESTER:
        ENVIRONMENT: staging
        CONFIG:
            DATABASE:
                HOST: 2123.3123.2112.12
                USERNAME: x
                PASSWORD: y
                DB: Q
            CLASSIFIER:
                IMG_WIDTH: 380
                IMG_HEIGHT: 380
                HOST: 0.0.0.0:3201
    

    UPDATE:
    If you want to use multiple configurations, then generally list of maps is used with a name attribute like below.
    It is a good practice to give NAME attribute, even if there is only one config.

    appName: test
    logLevel: WARN
    
    TESTER:
      - NAME: staging
        ENVIRONMENT: staging
        CONFIG:
          DATABASE:
            HOST: 2123.3123.2112.12
            USERNAME: x
            PASSWORD: 'y'
            DB: Q
          CLASSIFIER:
            IMG_WIDTH: 380
            IMG_HEIGHT: 380
            HOST: '0.0.0.0:3201'
      - NAME: production
        ENVIRONMENT: production
        CONFIG:
          DATABASE:
            HOST: 2123.3123.2112.14
            USERNAME: xP
            PASSWORD: yP
            DB: Q
          CLASSIFIER:
            IMG_WIDTH: 380
            IMG_HEIGHT: 380
            HOST: '0.0.0.0:3201'
    

    In this YAML, he similarly uses two maps under deploy