How to load environment variables in a config.ini file?

Question:

I have a config.ini file which contains some properties but I want to read the environment variables inside the config file.

[section1]
 prop1:(from envrinment variable) or value1

Is this possible or do I have to write a method to take care of that?

Asked By: user429035

||

Answers:

I know this is late to the party, but someone might find this handy.
What you are asking after is value interpolation combined with os.environ.

Pyramid?

The logic of doing so is unusual and generally this happens if one has a pyramid server, (which is read by a config file out of the box) while wanting to imitate a django one (which is always set up to read os.environ).
If you are using pyramid, then pyramid.paster.get_app has the argument options: if you pass os.environ as the dictionary you can use %(variable)s in the ini. Not that this is not specific to pyramid.paster.get_app as the next section shows (but my guess is get_app is the case).

app.py

from pyramid.paster import get_app
from waitress import serve
import os

app = get_app('production.ini', 'main', options=os.environ)
serve(app, host='0.0.0.0', port=8000, threads=50)

production.ini:

[app:main]
sqlalchemy.url = %(SQL_URL)s
...

Configparse?

The above is using configparser basic interpolation.

Say I have a file called config.ini with the line

[System]
conda_location = %(CONDA_PYTHON_EXE)

The following will work…

import configparser, os
cfg = configparser.ConfigParser()
cfg.read('config.ini')
print(cfg.get('System', 'conda_location', vars=os.environ))
Answered By: Matteo Ferla

I think :thinking_face, use .env and in config.py from dotenv import load_dotenv() and in next line load_dotenv() and it will load envs from .env file

Answered By: StackExchange

To load environment variables in a config.ini file, you can use a library that provides support for reading environment variables and config files. Here is an example using the Python python-dotenv library:

    # install the library
    pip install python-dotenv

    # create a .env file
    # In this file, you can store your environment variables in key=value format.
    # Example:
    # DEBUG=True
    # SECRET_KEY=abcdefghijklmnopqrstuvwxyz123456

    # In your code, you need to load the .env file using the load_dotenv function
    import os
    from dotenv import load_dotenv

    load_dotenv()

    # Now, you can access the environment variables in your code
    DEBUG = os.getenv("DEBUG")
    SECRET_KEY = os.getenv("SECRET_KEY")

    # In your config.ini file, you can access the environment variables like this:
    [settings]
    debug = ${DEBUG}
    secret_key = ${SECRET_KEY}

You can then use a library like configparser to load the config.ini file in your code:

import configparser

config = configparser.ConfigParser()
config.read('config.ini')

debug = config.get('settings', 'debug')
secret_key = config.get('settings', 'secret_key')
Answered By: Vinod Rawat
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.