title: Why globals() inside a function can't update variables in interactive shell?
body: <p>Today I wrote an "alias import function" for myself, because I need to write a script to do variable value checks for different python files.  </p>
  
  <pre><code># filename: zen_basic.py
  
  import importlib
  def from_module_import_alias(module_name, var_name, alias):
      """ equal to from module import a as b """
      agent = importlib.import_module(module_name)
      globals()[alias] = vars(agent)[var_name]
  </code></pre>
  
  <p>The weird thing is, if I start a Python interactive shell, I can't import things by using this function. But by using its content outside of the function, it works.</p>
  
  <pre><code>&gt;&gt;&gt; from zen_basic import *
  &gt;&gt;&gt; module_name = 'autor'
  &gt;&gt;&gt; var_name = 'food'
  &gt;&gt;&gt; alias = 'fd'
  &gt;&gt;&gt; from_module_import_alias(moduele_name, var_name, alias)
  Traceback (most recent call last):
    File "&lt;stdin&gt;", line 1, in &lt;module&gt;
  NameError: name 'moduele_name' is not defined
  &gt;&gt;&gt; from_module_import_alias(module_name, var_name, alias)
  &gt;&gt;&gt; fd
  Traceback (most recent call last):
    File "&lt;stdin&gt;", line 1, in &lt;module&gt;
  NameError: name 'fd' is not defined
  &gt;&gt;&gt; agent = importlib.import_module(module_name)
  &gt;&gt;&gt; globals()[alias] = vars(agent)[var_name]
  &gt;&gt;&gt; fd
  'cake'
  &gt;&gt;&gt;
  </code></pre>
  
  <p>I did 3 more experiments after:  </p>
  
  <ol>
  <li><ul>
  <li>Do <code>python -i test.py</code>  </li>
  <li><code>import zen_basic</code> in interactive shell  </li>
  <li>call <code>from_module_import_alias</code> function, failed.  </li>
  </ul></li>
  <li><ul>
  <li>Do <code>python -i zen_basic.py</code>  </li>
  <li>call <code>from_module_import_alias</code> function, success.</li>
  </ul></li>
  <li><ul>
  <li>add code which <code>import zen_basic</code> and call the <code>from_module_import_alias function</code> to <code>test.py</code></li>
  <li>Do <code>python -i test.py</code>, success</li>
  </ul></li>
  </ol>
  
  <p>What's the reason that directly using <code>from_module_import_alias</code> function in a Python interactive shell failed?</p>