title: How to mimic Python modules without import?
body: <p>I’ve tried to develop a « module expander » tool  for Python 3 but I've some issues.</p>
  
  <p>The idea is the following : for a given Python script <code>main.py</code>, the tool generates a functionally equivalent Python script <code>expanded_main.py</code>, by replacing each import statement by the actual code of the imported module; this assumes that the Python source code of the imported is accessible. To do the job the right way, I’m using the builtin module <code>ast</code> of Python as well as <code>astor</code>, a third-party tool allowing to dump the AST back into Python source. The motivation of this import expander is to be able to compile a script into one single bytecode chunk, so the Python VM should not take care of importing modules (this could be useful for MicroPython, for instance).</p>
  
  <p>The simplest case is the statement:</p>
  
  <pre><code>from import my_module1 import *
  </code></pre>
  
  <p>To transform this, my tool looks for a file <code>my_module1.py</code> and it replaces the import statement by the content of this file. Then, the <code>expanded_main.py</code> can access any name defined in <code>my_module</code>, as if the module was imported the normal way. I don’t care about subtle side effects that may reveal the trick. Also, to simplify, I treat <code>from import my_module1 import a, b, c</code> as the previous import (with asterisk), without caring about possible side effect. So far so good.</p>
  
  <p>Now here is my point. How could you handle this flavor of import:</p>
  
  <pre><code>import my_module2
  </code></pre>
  
  <p>My first idea was to mimic this by creating a class having the same name as the module and copying the content of the Python file indented:</p>
  
  <pre><code>class my_module2:
      # content of my_module2.py
      …
  </code></pre>
  
  <p>This actually works for many cases but, sadly, I discovered that this has several glitches: one of these is that it fails with functions having a body referring to a global variable defined in the module. For example, consider the following two Python files:</p>
  
  <pre><code># my_module2.py 
  g = "Hello"
  def greetings():
      print (g + " World!")
  </code></pre>
  
  <p>and</p>
  
  <pre><code># main.py
  import my_module2
  print(my_module2.g)
  my_module2.greetings()
  </code></pre>
  
  <p>At execution, <code>main.py</code> prints <code>"Hello"</code> and <code>"Hello World!"</code>. Now, my expander tool shall generate this:</p>
  
  <pre><code># expanded_main.py
  class my_module2:
      g = "Hello"
      def greetings():
          print (g + " World!")
  print(my_module2.g)
  my_module2.greetings()
  </code></pre>
  
  <p>At execution of <code>expanded_main.py</code>, the first print statement is OK (<code>"Hello"</code>) but the greetings function raises an exception: <code>NameError: name 'g' is not defined</code>.
  What happens actually is that</p>
  
  <ul>
  <li>in the <em>module</em> <code>my_module2</code>, <code>g</code> is a global variable,</li>
  <li>in the <em>class</em> <code>my_module2</code>, <code>g</code> is a class variable, which should be referred as <code>my_module2.g</code>.</li>
  </ul>
  
  <p>Other similar side effects happens when you define functions, classes, … in <code>my_module2.py</code> and you want to refer to them in other functions, classes, … of the same <code>my_module2.py</code>.</p>
  
  <p><em>Any idea how these problems could be solved?</em></p>
  
  <p><em>Apart classes, are there other Python constructs that allow to mimic a module?</em></p>
  
  <p>Final note: I’m aware that the tool should take care 1° of nested imports (recursion), 2° of possible multiple import of the same module. I don't expect to discuss these topics here.</p>