title: How to solve AttributeError: Can't get attribute on<module 'x.py'> error?
body: <p>I have two files <code>x.py</code> and <code>y.py</code>.</p>
  <p>Inside <code>y.py</code>, there are two classes, <code>A</code> and <code>B</code>. Class <code>A</code> calls class <code>B</code> inside <code>run</code> function.</p>
  <p>In file <code>x.py</code>, I imported class <code>A</code> to run it:</p>
  <pre><code>from y import A
  obj = A()
  obj.run()
  </code></pre>
  <p>I got this error:
  <code>AttributeError: Can't get attribute 'B' on &lt;module '__main__' from 'x.py'&gt;</code></p>
  <p>I found actually the solution here: <a href="https://stackoverflow.com/questions/45483349/attributeerror-cant-get-attribute-on-module-main-from-manage-py">AttributeError: Can't get attribute on &lt;module '<strong>main</strong>' from 'manage.py'&gt;</a>, which simple solves this error by importing class <code>B</code> inside file <code>x.py</code>:</p>
  <pre><code>from y import A, B
  </code></pre>
  <p>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.</p>
  <p>How can I solve this issue in the &quot;right way&quot; from a software engineer perspective?</p>
  <h1>Edit: adding an example</h1>
  <p>Class <code>B</code> is <code>RoBerta_CLS</code>.</p>
  <p>file <code>x.py</code>:</p>
  <pre><code># from y import RoBerta_CLS
  from y import A
  
  if __name__ == '__main__':
      obj = A(model_path='/home/PATH/models/DistilRoBERTa/')
  </code></pre>
  <p>file <code>y.py</code>:</p>
  <pre><code>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)
  </code></pre>