title: How to use built-in modules in a function of a string using exec()
body: <p>I am running a websocket python file. In that,I created built-in modules in a function using imp module and I am executing a function of string using exec() in another function.
  I use those built-in modules as globals parameter in exec().
  But when I use those modules in the function of string, it throws following error</p>
  <pre><code>'module' object has no attribute 'pose'
  </code></pre>
  <p>However,using those modules outside the function of string works perfectly and returns expected values. <strong>But how to use inside the function?</strong>.</p>
  <p>Here is the full code</p>
  <pre class="lang-py prettyprint-override"><code>import imp
  import sys
  import time
  
  def generate_modules():
      destination_module = imp.new_module(&quot;destination&quot;)
      destination_module.destination = imp.new_module(&quot;destination&quot;)
      destination_module.destination.pose = &quot;Hello World&quot;
      # Define GUI module
      gui_module = imp.new_module(&quot;GUI&quot;)
      gui_module.GUI = imp.new_module(&quot;GUI&quot;)
      gui_module.GUI.robotPose = lambda: &quot;robotPose&quot;
      sys.modules[&quot;destination&quot;] = destination_module
      sys.modules[&quot;GUI&quot;] = gui_module
      return gui_module,destination_module
  
  #Main function
  def process_code():
      gui_module,destination_module = generate_modules()
      builtin_modules = {&quot;GUI&quot;: gui_module,&quot;destination&quot;:destination_module,&quot;time&quot;: time}
      globl = globals()
      global_functions = globl.copy()
      global_functions.update(builtin_modules)
      sequential_code = &quot;&quot;&quot;from GUI import GUI
  from destination import destination
  def generatepath():
      data = destination.pose
      pose = GUI.robotPose()
      return data&quot;&quot;&quot;
      dic = {}
      exec(sequential_code,global_functions,dic)
      func = dic[&quot;generatepath&quot;]
      value = func()
      return value
  
  process_code()
  </code></pre>
  <p>Thank you, any help is appreciated</p>