Import a variable from a function within a class from one file to another file

Hi All, I have 2 files “utils.py” and “trainer.py”. The trainer.py has a class “Trainer” with a function “tnr” defined under the class as shown in the image below. I want to access the highlighted variables “edge_logits” and “nodes_logits” in the last line of the trainer.py and use them in the “reconstruction_scores” function in the utils.py file but I have tried all possible options but getting error. As can be seen, I have already imported utils in the trainer.py file and so when I import trainer in the utils.py, it will throw a error as a result of circular imports. Any ideas to help access the “nodes_logits” and “edges_logits” from the trainer.py and use them in the “reconstruction_scores” function in the utils.py are welcomed. Below are the images of the trainer.py and utils.py.

trainer.py
1

utils.py
2

You should be able to import MolecularMetrics and use it in trainer.py via:

...
edges_logits, nodes_logits = self.G(z)
MolecularMetrics.reconstruction_score(edges_logits, nodes_logits)

PS: you can post code snippets by wrapping them into three backticks ```, which makes debugging easier.

Hi @ptrblck. Thanks for the reply but I want to do the opposite of what you are thinking. I want to import from the trainer.py and use edge_logits and nodes_logits in the utils.py as highlighted under the reconstruction_scores function in the utils image. How do I go about it without incurring circular import errors.
I have already tried importing Trainer class in the utils.py by from trainer import Trainer and using Trainer.tnr.nodes_logits and Trainer.tnr.edges_logits in the reconstruction_scores function, but I get circular import error since I have also imported utils.py in the trainer.py.

ImportError: cannot import name 'Trainer' from 'trainer'

You won’t be able to access edges_logits or nodes_logits from Trainer.tnr as they are not attributes but local variables.
Maybe return these tensors and call the Trainer class from MolecularMetrics.
To avoid circular imports, check e.g this post.