title: if __name__ == '__main__' function call
body: <p>I am trying to work around a problem I have encountered in a piece of code I need to build on. I have a python module that I need to be able to import and pass arguments that will then be parsed by the main module. What I have been given looks like this:</p>
  
  <pre><code>#main.py
  if __name__ == '__main__' 
      sys.argv[] #pass arguments if given and whatnot
      Do stuff...
  </code></pre>
  
  <p>What I need is to add a <code>main()</code> function that can take argument(s) and parse them and then pass them on like so:</p>
  
  <pre><code>#main.py with def main()
  def main(args):
      #parse args
      return args
  
  if __name__ == '__main__':
      sys.argv[] #pass arguments if given and whatnot
      main(sys.argv)
      Do stuff...
  </code></pre>
  
  <p>To sum up: I need to import main.py and pass in arguments that are parsed by the <code>main()</code> function and then give the returned information to the <code>if __name_ == '__main_'</code> part.</p>
  
  <p><strong>EDIT</strong>
  To clarify what I am doing</p>
  
  <pre><code>#hello_main.py 
  import main.py
  
  print(main.main("Hello, main"))
  </code></pre>
  
  <p><strong>ALSO</strong> I want to still be able to call main.py from shell via</p>
  
  <pre><code>$: python main.py "Hello, main"
  </code></pre>
  
  <p>Thus preserving the <strong>name</strong> == <strong>main</strong> </p>
  
  <p>Is what I am asking even possible? I have been spending the better part of today researching this issue because I would like to, if at all possible, preserve the main.py module that I have been given. </p>
  
  <p>Thanks,</p>
  
  <p>dmg</p>