Applications require initialization before we can use them. This is true for most applications that we use everyday, from desktop, mobile to web applications. There are several ways to initialize applications. This guide focuses on the most common methods that we can use when writing Python applications.
Perhaps the most common way to save configurations is to write in Python modules. The configurations are commonly stored on variables and imported when needed. For example, if we have a module named
#!/usr/bin/env python HOST = 'mydomain.com' PORT = 8080 USERNAME = 'username' PASSWORD = 'password'
We can import these variables to initialize our application. For instance, logging in to a server, before using an app.
#!/usr/bin/env python from config import HOST, PORT, USERNAME, PASSWORD login(host=HOST, port=PORT, username=USERNAME, password=PASSWORD) # next steps here
Another popular method of configuring applications is by accessing environment variables. We can do this in Python using the
os standard library.
#!/usr/bin/env python import os login(host=os.getenv('HOST'), port=os.getenv('PORT'), username=os.getenv('USERNAME'), password=os.getenv('PASSWORD')) # next steps here
This is similar to the previous method, however, the variables written in
.env file and are loaded only at run time. The
.env file should contain the following:
HOST=mydomain.com PORT=8080 USERNAME=username PASSWORD=password
A popular library for loading a
.env file is by using python-dotenv. The first step is to install it using
pip install python-dotenv
To use python-dotenv, simply call load_dotenv() and use os standard library.
#!/usr/bin/env python import os from dotenv import load_dotenv load_dotenv() login(host=os.getenv('HOST'), port=os.getenv('PORT'), username=os.getenv('USERNAME'), password=os.getenv('PASSWORD')) # next steps here
Note: If you are using pipenv, there is no need to explicitly load a
.env file as
pipenv will automatically load this file. Just use the
os standard library.
.ini and .cfg files
CFG files are some of the oldest methods of storing configuration. Even the production-tested applications are still using this type of configuration. One advantage of
INI files is the support for sections. For example, the format below groups the variables to the section
[CREDENTIALS] HOST: mydomain.com PORT: 8080 USERNAME: username PASSWORD: password
These files can be read using the Python standard library, configparser.
#!/usr/bin/env python import configparser config = configparser.ConfigParser() config.read('config.ini') credentials = config['CREDENTIALS'] login(host=credentials['HOST'], port=credentials['PORT'], username=credentials['USERNAME'], password=credentials['PASSWORD']) # next steps here
Because YAML format supports a lot of structures, it is commonly used by most modern applications. The advantage of YAML files is that, unlike other plain text formats, it is not limited to just variables or sections but it has support for lists or maps, and the depth does not have a limit. This example is a simple representation of YAML.
CREDENTIALS: HOST: mydomain.com PORT: 8080 USERNAME: username PASSWORD: password
PyYAML is the most complete YAML parser implementation for Python. To install PyYAML, simply use
pip install pyyaml
The example below loads a YAML file.
#!/usr/bin/env python import yaml with open('config.yml') as f: configurations = yaml.safe_load(f.read()) credentials = configurations['CREDENTIALS'] login(host=credentials['HOST'], port=credentials['PORT'], username=credentials['USERNAME'], password=credentials['PASSWORD'])
Depending on the use cases, we can choose which configuration format we can use. Take note that this list only cover the most common and simplest methods. There are also other configuration formats available like JSON, Windows Registry, etc.python env ini yaml