6

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?

Mozahler
  • 4,958
  • 6
  • 36
  • 56
user429035
  • 411
  • 1
  • 5
  • 14
  • Possible duplicate of [How to read and write INI file with Python3?](https://stackoverflow.com/questions/8884188/how-to-read-and-write-ini-file-with-python3) – Rory Daulton Nov 16 '18 at 00:24
  • @RoryDaulton - not a duplicate! That one read the values from `ini file` whereas I want to read the environment values in `ini file`. So that I can switch to default if env variables aren't available. – user429035 Nov 16 '18 at 00:43
  • Your purpose is still not clear. By definition, an "environment variable" is a value held by the operating system (Windows or DOS), not in a file. However, you are able to store any regular text, such as contents of an environment variable, in an `ini` file. What do you want that is not easily doable in the other question? Perhaps you should explain your purpose in more detail. – Rory Daulton Nov 16 '18 at 01:04
  • This question is perfectly clear to me as a Linux programmer. A Linux "environment variable" is a name-value binding provided by linux -- for example, the response to `printenv FOOBAR` from a command line. I think this question is "How do I use the value of a (Linux) system environment variable inside a a Python config file?". – Tom Stambaugh Sep 09 '22 at 16:10

2 Answers2

9

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))
Matteo Ferla
  • 2,128
  • 1
  • 16
  • 27
-1

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