title: Import function from a module to global
body: <p>I am using Jupyter Notebook for some ad-hoc analysis. I wrap some codes up using functions so my notebook will not be very long. However, when I try to move functions to a module (*.py) and import it into my notebook, those functions cannot access the global variables I define in the notebook.</p>
  <pre><code>a = 1
  
  def func_in_notebook():
    print(a)
  
  func_in_notebook()
  </code></pre>
  <p>This works; but when I try to import the function from a module, it doesn't work:</p>
  <p>Firstly I save the following as &quot;utility.py&quot;</p>
  <pre><code>def func_from_module():
    print(a)
  </code></pre>
  <p>And the following gives me the error:</p>
  <pre><code>from utility import func_from_module
  a = 1
  func_from_module()
  </code></pre>
  <p><code>NameError: name 'a' is not defined</code></p>
  <p>I know I can simply add &quot;a&quot; as the function argument, <strong>but in reality, I may have more than 20 global variables I need for my function to run</strong>, and I hope to avoid identifying all global variables I need for the function to work. Is there a way I can import the function from a module to the global, so the f has the access to global variables; or simply make the module see all global variables I define in the notebook?</p>
  <p>Thanks in advance.</p>