ARTICLE AD BOX
New to Python, writing some practical programs to handle personal excel file processing. Get confused on how Python load the dependency files.
Have some log functionalities writing in a separate file:
# directory: common.util; file name:common_util.py if os.path.exists("folder/logging.log"): # remove previous logging file os.remove("folder/logging.log") logFile: TextIOWrapper = open("folder/logging.log", "a", encoding = "utf-8") def log_activity(message: str) -> None: logFile.write(f"{message}\n")Use the logging method from a module:
from common.util import log_activity def data_cleaning() -> None: log_activity(message = "Data cleaning starts.") parse_excel() clean_data() generate_excel() log_activity(message = "Data cleaning ends.")Import above module from main file:
from process.data_cleaning import data_cleaning def process() -> None: data_cleaning() data_staging() data_analysis() if __name__ == "__main__": process()I do notice that log file is refreshed (as expected) everytime I am running the main file. Just want to know:
How Python load the common_util.py file?
Are all the lines of code (not the ones in the method) getting executed when the file is loaded?
When the file that loads the common_util.py gets loaded by other file, will the all the lines of code (not the ones in the method) in common_util.py get executed again?
Appreciated
