title: Problem with sys.argv[1] when unittest module is in a script
body: <p>I have a script that does various things and access paramenters using sys.argv but when the script gets to the unittest part of the code it says there is no module for this. The script that I have is:</p>
  
  <pre><code>class MyScript():
  
      def __init__(self):
          self.value = sys.argv[1]
  
      def hello(self):
          print self.value
  
      def suite(self):
          modules_to_test = ('external_sanity_onvif', 'starttest')
          alltests = unittest.TestSuite()
          for module in map(__import__, modules_to_test):
              alltests.addTest(unittest.findTestCases(module))
          return alltests
  
  
  if __name__ == '__main__': 
      Run = MyScript()
      Run.hello()
      log_file = 'log_file.txt'
      test_file = open(log_file, "w") 
      runner = unittest.TextTestRunner(test_file)
      unittest.main(defaultTest='Run.suite', testRunner=runner)
  </code></pre>
  
  <p>Say I enter <code>./script.py Hello</code> in the command line. The error I get is:</p>
  
  <pre><code>AttributeError: 'module' object has no attribute 'Hello'
  </code></pre>
  
  <p>If I remove the unittest module it works. Also if I remove the testrunner log and leave it at:</p>
  
  <pre><code>unittest.main(defaultTest='Run.suite')
  </code></pre>
  
  <p>This still doesn't work.</p>
  
  <p>Can anyone help.</p>
  
  <p>Thanks</p>
  
  <p>I tried this hack but it still tries to read the <code>sys.argv</code>.</p>
  
  <pre><code>project = sys.argv[4:]
  sys.argv = sys.argv[0:4]
  </code></pre>
  
  <p>I have tried this with just <code>argv</code> but it still tires to read the extra parameters.</p>