title: importing other files in python and global variables
body: <p>I cannot quite find a good description on how import works when importing your own files.  </p>
  
  <p>I was having trouble importing a file with a global variable and managed to get it to work when I put the global variable just before the files main function.  </p>
  
  <p>Can someone explain why it works this way?  </p>
  
  <p>A quick run down on how import actually works. </p>
  
  <p>It did not work when I did this (pseudocode): </p>
  
  <p><strong>file1:</strong></p>
  
  <pre><code>import file2    
  file2.main()
  </code></pre>
  
  <p><strong>file2:</strong></p>
  
  <pre><code>main():
      glob_var = 0
  
      def add():
          global glob_var
          glob_var += 1
  
      add()
  </code></pre>
  
  <p>But worked if I put the variable first like this:</p>
  
  <p><strong>file1:</strong></p>
  
  <pre><code>import file2    
  file2.main()
  </code></pre>
  
  <p><strong>file2:</strong></p>
  
  <pre><code>glob_var = 0
  
  main(): 
      def add():
          global glob_var
          glob_var += 1
  
      add()
  </code></pre>