title: Replace function in main module from imported module in Python
body: <p>Is it possible to set a global variable in the main module from an imported module (using a string) in python?</p>
  
  <p>(Yes I know this isn't best practice.)</p>
  
  <p>Ultimately I want it to look something like this:</p>
  
  <p><code>main.py</code></p>
  
  <pre><code>import mod
  def func():
    print('failure')
  mod.run(func)
  func()
  </code></pre>
  
  <p><code>mod.py</code></p>
  
  <pre><code>def func2():
      print('success')
  def run(f):
      globals()[f.__name__] = func2
  </code></pre>
  
  <p>The result is <code>'failure'</code> because global is relative to the module.</p>
  
  <p>I'm wanting to overwrite the variable <code>func</code> with <code>func2</code>, from the module.</p>
  
  <p>Another caveat: the variable <code>func</code> changes, so I need to refer to it be the <code>f.__name__ string</code></p>
  
  <p>I'm aware that this approach wouldn't work if the name of <code>func</code> were changed before it's changed via <code>mod.run(func)</code>.</p>
  
  <p><strong>My question:</strong>  Is it possible to change the function in the main module from an imported module, without changing the code in the above example <code>main.py</code>?  If so, how?</p>