title: How to import class from different script, where the class uses a global variable at initialization?
body: <p>From one file, I'm trying to import and initialize a class from another file, where that class is initialized with a global variable defined in the calling file.</p>
  <p>My file setup looks like this.</p>
  <pre><code>folder
  ├──subfolder
  │   └──  __init__.py
  │   └──  sub.py
  ├──__init__.py
  ├──orig.py
  </code></pre>
  <p>My <code>orig.py</code> file looks like this:</p>
  <pre><code>from folder.subfolder.sub import Test
  
  def varinit():
      global var
      var = 8
  
  def runn():
      varinit()
      testInstance = Test()
      testInstance.print_modvar()
  
  if __name__ == &quot;__main__&quot;:
  
      runn()
  </code></pre>
  <p>My <code>sub.py</code> file looks like this:</p>
  <pre><code>from folder.orig import var
  
  class Test():
      def __init__(self):
          self.mod_var=var+8
  
      def print_modvar(self):
  
          print(self.mod_var)
  </code></pre>
  <p>In my terminal, I set:</p>
  <pre><code>export PYTHONPATH=/path/to/rootfolder
  </code></pre>
  <p>Where <code>rootfolder</code> contains folder.</p>
  <p>When I run <code>python3 folder.orig.py</code> I get this error:</p>
  <pre><code>Traceback (most recent call last):
    File &quot;folder/orig.py&quot;, line 1, in &lt;module&gt;
      from folder.subfolder.sub import Test
    File &quot;/content/folder/subfolder/sub.py&quot;, line 1, in &lt;module&gt;
      from folder.orig import var
    File &quot;/content/folder/orig.py&quot;, line 1, in &lt;module&gt;
      from folder.subfolder.sub import Test
  ImportError: cannot import name 'Test'
  </code></pre>
  <p>The issue is that it is not able to locate <code>sub.py</code>? Or is it able to locate <code>sub.py</code>, but just not the class defined in <code>sub.py</code>?</p>
  <p>How can I modify this to be able to correctly <code>import</code> the class, with it correctly using the global variable at initialization?</p>
  <p>The result is that it should print the number <code>16</code>.</p>
  <p>For convenience I have a colab notebook with the code that is interactive. The files likely won't persist though</p>
  <p><a href="https://colab.research.google.com/drive/1mYk-XTpQh5d8IoTzXg9phGc2WupYKlRU?usp=sharing" rel="nofollow noreferrer">https://colab.research.google.com/drive/1mYk-XTpQh5d8IoTzXg9phGc2WupYKlRU?usp=sharing</a></p>