title: Importing a python script/module that uses argparse into another python script
body: <p>This question may be regarded to be redundant but, to my defense, I've considered similar questions and given solutions but they don't work for me (or I just didn't understand them) as I will show.</p>
  
  <p><strong>What I want to do:</strong>
  I wrote a couple of python scripts that are each command-line tools (accepting several arguments using argparse) when built with pyinstaller. They all work as expected both from within pycharm's terminal and from the Ubuntu terminal. Now, I would like each of these scripts to be a module that could be called from another python script passing required arguments as I would do in the terminal.</p>
  
  <p><strong>This is one of the original scripts</strong> (I've shortened the script a bit so that it serves as a minimal example):</p>
  
  <pre><code>import sys
  import argparse
  import getpass
  
  
  if len(sys.argv) &lt; 2:
      print "You haven't specified any arguments. Use -h to get more details on how to use this command."
      sys.exit(1)
  
  
  parser = argparse.ArgumentParser()
  parser.add_argument('--username', '-u', type=str, default=None, help='Username for the login to the WebCTRL server')
  parser.add_argument('--password', '-p', type=str, default=None, help='Password for the login to the WebCTRL server')
  parser.add_argument('--node', '-n', type=str, default=None,
      help='Path to the point or node whose children you want to retrieve. Start querying at the lowest level with "-n /trees/geographic"')
  parser.add_argument('-url', type=str, default='https://my.server.de',
      help="URL of the WebCTRL server as e.g. http://google.de")
  args = parser.parse_args()
  
  
  if args.username is None:
      print 'No user name specified. Login to WebCTRL needs a user name and password. Check all options for this command via -h'
      sys.exit(1)
  else:
      username = args.username
  if args.password is None:
      password = getpass.getpass('No password specified via -p. Please enter your WebCTRL login password: ')
  else:
      password = args.password
  if args.node is None:
      print 'No path to a node specified. Check all options for this command via -h'
      sys.exit(1)
  if args.url is None:
      print 'No URL given. Specify the URL to the WebCTRL server analogous to http://google.de'
      sys.exit(1)
  else:
      wsdlFile = args.url + '/_common/webservices/Eval?wsdl'
  
  
  # This doesn't belong to my original code. It's rather for demonstration:
  # Print the arguments and leave the script
  print 'Username: ' + args.username
  print 'Node: ' + args.node
  print 'URL: ' + args.url
  
  sys.exit(0)
  </code></pre>
  
  <p>As I said: From within my IDE (Pycharm) ... </p>
  
  <pre><code>$ python wc_query_test.py -u wsdl -n /trees/geographic
  No password specified via -p. Please enter your WebCTRL login password: 
  Username: wsdl
  Node: /trees/geographic
  URL: https://my.server.de
  </code></pre>
  
  <p>... and from the Ubuntu terminal it works just fine:</p>
  
  <pre><code>$ pyinstaller --distpath dist/. wc_query_test.py
  $ ./dist/wc_query_test/wc_query_test -u wsdl -n /trees/geographic
  No password specified via -p. Please enter your WebCTRL login password: 
  Username: wsdl
  Node: /trees/geographic
  URL: https://my.server.de
  </code></pre>
  
  <p><strong>Coming to the actual problem:</strong>
  I want the script wc_query_test.py to be a module that can be imported into another python script and executed there passing along the arguments as I do in the command line. To achieve this I followed @Waylan's instructions in this <a href="https://stackoverflow.com/questions/28440441/importing-a-python-script-from-another-script-and-running-it-with-arguments">stackoverflow question</a>.</p>
  
  <p>This is the code I came up with (wc_query_test.py):</p>
  
  <pre><code>import sys
  import argparse
  import getpass
  
  def main(**kwargs):
      if kwargs.username is None:
          print 'No user name specified. Login to WebCTRL needs a user name and password. Check all options for this command via -h'
          sys.exit(1)
      else:
          username = kwargs.username
      if kwargs.password is None:
          password = getpass.getpass('No password specified via -p. Please enter your WebCTRL login password: ')
      else:
          password = kwargs.password
      if kwargs.node is None:
          print 'No path to a node specified. Check all options for this command via -h'
          sys.exit(1)
      if kwargs.url is None:
          print 'No URL given. Specify the URL to the WebCTRL server analogous to http://google.de'
          sys.exit(1)
      else:
          wsdlFile = kwargs.url + '/_common/webservices/Eval?wsdl'
  
      # This doesn't belong to my original code. It's rather for demonstration:
      # Print the arguments and leave the script
      print 'Username: ' + username
      print 'Node: ' + kwargs.node
      print 'URL: ' + kwargs.url
  
      sys.exit(0)
  
  def run():
      parser = argparse.ArgumentParser()
      parser.add_argument('--username', '-u', type=str, default=None, help='Username for the login to the WebCTRL server')
      parser.add_argument('--password', '-p', type=str, default=None, help='Password for the login to the WebCTRL server')
      parser.add_argument('--node', '-n', type=str, default=None,
          help='Path to the point or node whose children you want to retrieve. Start querying at the lowest level with "-n /trees/geographic"')
      parser.add_argument('-url', type=str, default='https://my.server.de',
          help="URL of the WebCTRL server as e.g. http://google.de")
      args = parser.parse_args()
  
      main(**args)
  </code></pre>
  
  <p>... and the script that imports the module and calls it (test.py):</p>
  
  <pre><code>import wc_query_test
  wc_query_test.main(username='wsdl', password='aaaaaa', node='/trees/geographic')
  </code></pre>
  
  <p>When I run it in the python terminal I get:</p>
  
  <pre><code>~/PycharmProjects/webctrl$ python wc_query_test.py
  ~/PycharmProjects/webctrl$ python test.py 
  Traceback (most recent call last):
    File "test.py", line 3, in &lt;module&gt;
      wc_query_test.main(username='wsdl', password='aaaaaa', node='/trees/geographic/#geb_g', url='https://webctrl.rz-berlin.mpg.de')
    File "/home/stefan/PycharmProjects/webctrl/wc_query_test.py", line 23, in main
      if kwargs.username is None:
  AttributeError: 'dict' object has no attribute 'username'
  </code></pre>
  
  <p>Running wc_query_test.py yields no output and I see why this is. That was just a test. But running test.py yields an error as well. I get an idea why this can't work either but I cannot put it into words. What about this run() method? Does it make sense to have it? How would I need to modify my code in order to obtain this dual functionality as described in "What I want to do:"? Thank you in advance for your help!</p>
  
  <p><strong>UPDATE:</strong>
  I got rid of the error message. I changed e.g. <code>kwargs.username</code> to <code>kwargs.get('username')</code> since kwargs is a dictionary. The code now looks like this:</p>
  
  <pre><code>import sys
  import argparse
  import getpass
  
  def main(**kwargs):
      if kwargs.get('username') is None:
          print 'No user name specified. Login to WebCTRL needs a user name and password. Check all options for this command via -h'
          sys.exit(1)
      else:
          username = kwargs.get('username')
      if kwargs.get('password') is None:
          password = getpass.getpass('No password specified via -p. Please enter your WebCTRL login password: ')
      else:
          password = kwargs.get('password')
      if kwargs.get('node') is None:
          print 'No path to a node specified. Check all options for this command via -h'
          sys.exit(1)
      if kwargs.get('url') is None:
          print 'No URL given. Specify the URL to the WebCTRL server analogous to http://google.de'
          sys.exit(1)
      else:
          wsdlFile = kwargs.get('url') + '/_common/webservices/Eval?wsdl'
  
      # This doesn't belong to my original code. It's rather for demonstration:
      # Print the arguments and leave the script
      print 'Username: ' + username
      print 'Node: ' + kwargs.get('node')
      print 'URL: ' + kwargs.get('url')
  
      sys.exit(0)
  
  def run():
      parser = argparse.ArgumentParser()
      parser.add_argument('--username', '-u', type=str, default=None, help='Username for the login to the WebCTRL server')
      parser.add_argument('--password', '-p', type=str, default=None, help='Password for the login to the WebCTRL server')
      parser.add_argument('--node', '-n', type=str, default=None,
          help='Path to the point or node whose children you want to retrieve. Start querying at the lowest level with "-n /trees/geographic"')
      parser.add_argument('-url', type=str, default='https://webctrl.rz-berlin.mpg.de',
          help="URL of the WebCTRL server as e.g. http://google.de")
      args = parser.parse_args()
  
      main(**args)
  </code></pre>
  
  <p>Running it in the python terminal yields as expected:</p>
  
  <pre><code>$ python test.py 
  Username: wsdl
  Node: /trees/geographic
  URL: https://my.server.de
  </code></pre>
  
  <p>But building it via pyinstaller and running it as command line tool there is no output:</p>
  
  <pre><code>~/PycharmProjects/webctrl$ ./dist/wc_query_test/wc_query_test -h
  ~/PycharmProjects/webctrl$
  </code></pre>
  
  <p>How can I modify wc_query_test.py so that it takes arguments and serves as command line tool?</p>