May-07-2022, 01:26 PM
Below I have the files **main.py**, **train.py** and **dataLoader.py**. The purpose of path.py is to contain relevant paths inside my project. These paths however should not be hard-coded as multiple user on various machines may use it. The relative structure of the project stays the same on each system up to the base paths of the project. For example, the root folder for data, for evaluation, for the python etc. may differ though.
I want to use the following idea: The main gets an argument of its current user. This user is used too read in a config file setting the respective root paths of the project. Afterwards the interesting/relevant code of the project is executed, e.g. the dataloader.py. This code then uses paths.py and knows where to look up certain directories etc.
My code is working, but has certain caveats and in fact has to be adapted. I dislike my solution, because paths.py and interesting/relevant modules are imported inside the main function of main.py. I also feel uncomfortable that I have to set a new environment variable. Furthermore, the solution gets exceedingly more ugly, if I had to read in several paths from my config file as then each of them would again need their own environment variable.
Are there alternatives?
I want to use the following idea: The main gets an argument of its current user. This user is used too read in a config file setting the respective root paths of the project. Afterwards the interesting/relevant code of the project is executed, e.g. the dataloader.py. This code then uses paths.py and knows where to look up certain directories etc.
My code is working, but has certain caveats and in fact has to be adapted. I dislike my solution, because paths.py and interesting/relevant modules are imported inside the main function of main.py. I also feel uncomfortable that I have to set a new environment variable. Furthermore, the solution gets exceedingly more ugly, if I had to read in several paths from my config file as then each of them would again need their own environment variable.
Are there alternatives?
##main.py import configparser import os if __name__ == '__main__': # the section to read could be determined by args # rootDir= "rootDirOnTheServer" rootDir= "rootDirAtHome" config = configparser.ConfigParser() config.read(filenames="paths") os.environ["MYVAL"] = rootDir import path from dataLoader import loadData train() print("path.getRoot() :", path.getRoot()) print("path.subdirOfTheProject.base : ", path.subdirOfTheProject.base) print("Printing the path of dirOfTheProject:", path.dirOfTheProject.base) ########################################### ##paths.py import os def getRoot(): val = os.environ["MYVAL"] return val class dirOfTheProject: base = getRoot() + os.sep + "inside the project dir" class subdirOfTheProject: base = dirOfTheProject.base + os.sep + "inside one of its subfoldrs" ####################################################### ##dataLoader.py import path def loadData(): src = path.subdirOfTheProject.base print("Here are all my data files: ", src)