I have two files x.py
and y.py
.
Inside y.py
, there are two classes, A
and B
. Class A
calls class B
inside run
function.
In file x.py
, I imported class A
to run it:
from y import A
obj = A()
obj.run()
I got this error:
AttributeError: Can't get attribute 'B' on <module '__main__' from 'x.py'>
I found actually the solution here: AttributeError: Can't get attribute on <module 'main' from 'manage.py'>, which simple solves this error by importing class B
inside file x.py
:
from y import A, B
I feel this is not the right way to do it if I want to build a python library. I don't think you need always to do that when you call a class from a library.
How can I solve this issue in the "right way" from a software engineer perspective?
Edit: adding an example
Class B
is RoBerta_CLS
.
file x.py
:
# from y import RoBerta_CLS
from y import A
if __name__ == '__main__':
obj = A(model_path='/home/PATH/models/DistilRoBERTa/')
file y.py
:
import torch, os
import torch.nn as nn
from transformers import RobertaForSequenceClassification
device = 'cuda'
class RoBerta_CLS(torch.nn.Module):
def __init__(self, model_params):
super().__init__()
self.encoder = RobertaForSequenceClassification.from_pretrained(model_params['MODEL'], num_labels=1)
self.encoder = self.encoder.to(device)
def save_pretrained(self, output_model_file):
torch.save(self, output_model_file + 'pytorch_model.pt')
print('saved..')
@staticmethod
def from_pretrained(output_model_file):
model = torch.load(os.path.join(output_model_file, 'pytorch_model.pt'))
print('loaded..')
return model
class A:
def __init__(self, model_path=''):
self.model = RoBerta_CLS.from_pretrained(model_path)