How to use external configuration file in Python

While developing applications, the code might be uses confidential data like login credentials. Its not a secure way to save all the data in the script itself. So we can keep such things in a seperate configuration file and include that file in the scripts.

The module 'configparser' provides a better way to use external configurations in Python applications.

This aricle explains the sample usage of 'configparser' module.

Configuration File (config.ini)

mysql-admin = admin
mysql-pass = 9FNAh5PeUJZcE

Python Script (

import configparser
config = configparser.ConfigParser()'/etc/config.ini')
mysql_admin = config['DEFAULT']['mysql-admin']
mysql_pass = config['DEFAULT']['mysql-pass']

By this way we can load the confidential data saved in the configuration file to the script.

Leave a Reply