I am trying to call python script from bash script. (Note: I am using python version 3.7) Following is the Directory structure (so_test is a directory)
so_test
shell_script_to_call_py.sh
main_file.py
log_settings.py
files are as below,
shell_script_to_call_py.sh
#!/bin/bash
echo "...Enable Debug..."
python $(cd $(dirname ${BASH_SOURCE[0]}) && pwd)/main_file.py "input_1" --debug
echo "...No Debug..."
python $(cd $(dirname ${BASH_SOURCE[0]}) && pwd)/main_file.py "input_2"
main_file.py
import argparse
import importlib
importlib.import_module("log_settings.py")
from so_test import log_settings
def func1():
log.info("INFO Test")
log.debug("DEBUG Test")
log.warning("WARN Test")
def func2():
log.info("INFO Test")
log.debug("DEBUG Test")
log.warning("WARN Test")
def main():
parser = argparse.ArgumentParser()
parser.add_argument("input", type=str, help="input argument 1 is missing")
parser.add_argument("--debug", help="to print debug logs", action="store_true")
args = parser.parse_args()
log_settings.log_conf(args.debug)
log.info("INFO Test")
log.debug("DEBUG Test")
log.warning("WARN Test")
func1()
func2()
if __name__ == "__main__":
main()
log_settings.py
import logging
from colorlog import ColoredFormatter
def log_config(is_debug_level):
log_format = "%(log_color)s %(levelname)s %(message)s"
if is_debug_level:
logging.root.setLevel(logging.DEBUG)
else:
logging.root.setLevel(logging.INFO)
stream = logging.StreamHandler()
stream.setFormatter(ColoredFormatter(log_format))
global log
log = logging.getLogger('pythonConfig')
log.addHandler(stream)
Following are 2 issues I am facing. (as a newbie to python)
- I am not able to import the log_settings.py properly in main_file.py
- I want to access use log.debug, log.info etc. in main_file (and other .py file) across different functions, for which the settings (format, color etc.) is declared in log_settings.py file.