'What is an alternative way initializing the project's paths structure?
I have a project with a file paths.py that replicates the overall project structure. One path in this file has to be set upon executing main.py. After the path is set, one can then use all the other modules of the project that now know of the correct root of the project and its substructures.
Below I have a working version of this behaviour, but I don't like it because of two things:
First, it sets a new environmental variable. Secondly, it imports inside the main function. Personally, I don't think this is good coding style, thus I am wondering if there are alternatives to achieve the same result, but better/neater code.
# main.py
import os
if __name__ == '__main__':
os.environ["MYROOT"] = "SectionOne" # could be set via configparser or commandline
from busy import train
train()
###########################################
##paths.py
import os
def getRoot():
val = os.environ["MYROOT"]
return val
class A:
base = getRoot() + os.sep + "insideA"
class A2:
base = A.base + os.sep + "insideA2"
#######################################################
##busy.py
import paths
def train():
src = paths.A.base
print("train(): ", src)
print("path.getRoot() :", paths.getRoot())
print("path.A.base : ", paths.A.base)
print("path.A.base :", paths.A2.base)
# output is correct
# train(): SectionOne\insideA
# path.getRoot() : SectionOne
# path.A.base : SectionOne\insideA
# path.A.base : SectionOne\insideA\insideA2
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
