Search code examples
pythonenvironment-variablesfastapiconfigparserfaust

Can't Access configparser environment variables from env.ini file in faust (kafka streaming)


My Project is on FastAPI and the structure goes like this.

- project
   - app
      - kafka_layer
          - faustworker.py
   - core
      - configs.py
   - env.ini

My env.ini file goes like this

[DEFAULT]
DATABASE_URL=url_to_db

I've tried adding this code in configs.py

import configparser

config = configparser.ConfigParser()
config.read("env.ini")

I want to access environment variables in faustworker.py using this code

from app.core.configs import config
db_url = config['DEFAULT']['DATABASE_URL']

When I tried using the instance of config in faustworker.py, it returned KeyError: 'DATABASE_URL'


Solution

  • I was trying to run faust worker in a docker container going to the folder

    eg:

    cd app
    cd kafka_layer
    faust -A faust_worker
    

    But What I needed to do is

    faust -A app.kafka_layer.faust_worker
    

    It works totally fine now. :)