I have some files with the following structures. I would like to set device
in train.py
so the change reflects in eval.py
but it doesn't and it probably execute common.py
independently. I prefer not to import none common files in each other. However, I may import eval
in train
to do evaluation after train.
What is the solution or best practice for my scenario?
common.py
device = "cpu"
train.py
from common import *
from eval import *
import click
@click.command()
@click.argument("dev", type=str)
def train(dev):
global device
device = dev
eval()
if __name__ == "__main__":
train() # it prints "cpu"
eval.py
from common import *
def eval():
print(device) # I expect to have 'cuda' here
Now
$ python train.py cuda
$ cpu