How to switch the configuration file to be read by Python

Introduction

It is a method to change the configuration file to be read by adding an option when executing a program in Python.

file organization

.
├── config
│   ├── config_dev.ini
│   └── config_prod.ini
└── main.py

setting file

confg_prod.ini


[MYSQL]
USER = "user_prod"
PWD = "password_prod"

config_dev.ini


[MYSQL]
USER = "user_dev"
PWD = "password_dev"

Executable file

main.py


import argparse
import configparser


def parser_conf():
    parser = argparse.ArgumentParser()
    parser.add_argument(
        "-p",
        "--prod",
        action="store_true",
        help="use prod config")
    parser.add_argument(
        "-d",
        "--dev",
        action="store_true",
        help="use dev config")
    args = parser.parse_args()
    config = configparser.ConfigParser()
    if args.prod:
        config_file_name = "config/config_prod.ini"
    else:
        config_file_name = "config/config_dev.ini"
    config.read(config_file_name, encoding='utf-8')
    return config


if __name__ == '__main__':
    config = parser_conf()
    print(config['MYSQL']['USER'])
    print(config['MYSQL']['PWD'])

Execution result

$ python main.py -d
"user_dev"
"password_dev"

$ python main.py --dev
"user_dev"
"password_dev"

$ python main.py -p
"user_prod"
"password_prod"

$ python main.py --prod
"user_prod"
"password_prod"

Recommended Posts

How to switch the configuration file to be read by Python
Read the xml file by referring to the Python tutorial
Read the file line by line in Python
Read the file line by line in Python
How to make a command to read the configuration file with pyramid
How to read a CSV file with Python 2/3
[Python] How to read excel file with pandas
How to erase the characters output by Python
[Python Kivy] How to get the file path by dragging and dropping
Template of python script to read the contents of the file
How to read all the classes contained in * .py in the directory specified by Python
How to read the SNLI dataset
How to get the Python version
[Python] How to import the library
How to read an Excel file (.xlsx) with Pandas [Python]
How to read text by standard input or file name specification like cat in Python
[Python] How to convert db file to csv
How to read pydoc on python interpreter
How to convert Python to an exe file
Let's read the RINEX file with Python ①
Read the file by specifying the character code.
How to switch python versions in cloud9
[Python] Read the specified line in the file
How to easily switch the virtual environment created by Conda on Jupyter
[LPIC 101] How to specify the disk partition number in the GRUB configuration file
[Python] How to read a csv file (read_csv method of pandas module)
Switch the module to be loaded for each execution environment in Python
How to automatically notify by phone when the python system is down
[Implementation example] Read the file line by line with Cython (Python) from the last line
How to sort by specifying a column in the Python Numpy array.
[python] How to display list elements side by side
[Python] How to change the date format (display format)
How to create a JSON file in Python
[Python] How to read data from CIFAR-10 and CIFAR-100
[Algorithm x Python] How to use the list
How to get the files in the [Python] folder
[Python] How to sort instances by instance variables
Read line by line from a file with Python
How to read a file in a different directory
Various ways to read the last line of a csv file in Python
Bucket / file operations (transfer / acquire / delete / read, etc.) to GCS by python Summary
High resolution acoustic signal processing (1) --How to read 24-bit wav file with Python
[python] How to sort by the Nth Mth element of a multidimensional array
Fourier transform the wav file read by Python, reverse transform it, and write it again.
How to install a Python library that can be used by pharmaceutical companies
How to install Python
How to read PyPI
How to install python
How to read JSON
Read Python csv file
Read the Python-Markdown source: How to create a parser
The avi file output by OpenCV cannot be played
[Python] How to remove duplicate values from the list
How to retrieve the nth largest value in Python
How to get the variable name itself in python
Think about how to program Python on the iPad
How to get the number of digits in Python
[Python] Try to read the cool answer to the FizzBuzz problem
[Introduction to Python] How to iterate with the range function?
How to know the current directory in Python in Blender
I tried to touch the CSV file with Python