title: Read and write a variable in module A from module B
body: <p>Need a solution or workaround to read and write the variable in moduleA from moduleB please. Here is the code:</p>
  
  <p><strong>moduleA</strong></p>
  
  <pre><code>import moduleB
  
  variable = 10
  
  def changeVariable():
      global variable
      variable = 20
  
  def main():
      print("From moduleA, variable =", variable, " before change.")
      moduleB.main()
      print("From moduleA, variable =", variable, " after change.")
  
  if __name__ == "__main__":
      main()
  </code></pre>
  
  <p><strong>moduleB</strong></p>
  
  <pre><code>import moduleA
  
  def main():
      print("From moduleB, variable =", moduleA.variable, " before change.")
      moduleA.variable = 20 # Try 1
      moduleA.changeVariable() # Try 2
      print("From moduleB, variable =", moduleA.variable, " after change.")
  
  if __name__ == "__main__":
      main()
  </code></pre>
  
  <p>When running moduleA we get:</p>
  
  <ul>
  <li>From moduleA, variable = 10 before change.</li>
  <li>From moduleB, variable = 10 before change.</li>
  <li>From moduleB, variable = 20 after change.</li>
  <li>From moduleA, variable = 10 after change.</li>
  </ul>
  
  <hr>
  
  <p>Here is another example using static variables:</p>
  
  <p><strong>moduleAA</strong></p>
  
  <pre><code>import moduleBB
  
  class AA:
      variable = 0
  
      @staticmethod
      def changeVariable():
          AA.variable = 20
  
  def main():
      AA.variable = 10
      print("From moduleAA, variable =", AA.variable, " before change.")
      moduleBB.main()
      print("From moduleAA, variable =", AA.variable, " after change.")
  
  if __name__ == "__main__":
      main()
  </code></pre>
  
  <p><strong>moduleBB</strong></p>
  
  <pre><code>import moduleAA
  
  def main():
      print("From moduleB, variable =", moduleAA.AA.variable, " before change.")
      moduleAA.AA.variable = 20 # Try 1
      moduleAA.AA.changeVariable() # Try 2
      print("From moduleB, variable =", moduleAA.AA.variable, " after change.")
  
  if __name__ == "__main__":
      main()
  </code></pre>
  
  <p>When running moduleAA we get:</p>
  
  <ul>
  <li>From moduleAA, variable= 10  before change.</li>
  <li>From moduleBB, variable= 0  before change.</li>
  <li>From moduleBB, variable= 20  after change.</li>
  <li>From moduleAA, variable= 10  after change.</li>
  </ul>