title: How to use one Python script to run another Python script and pass variables to it?
body: <p>I have one Python script. Let's call it <strong>controller.py</strong>. I'd like to use <strong>controller.py</strong> to run another Python script and pass several variables to it. Let's call the second script <strong>analyzer.py</strong>. </p>
  
  <p>What is the best way to do this without importing <strong>analyzer.py</strong> as module? And how do I reference the variables I'm passing to <strong>analyzer.py</strong> within that script?</p>
  
  <p>Here's my failed attempt using subprocess:</p>
  
  <p><strong>controller.py</strong></p>
  
  <pre><code>import subprocess
  
  var1='mytxt'
  var2=100
  var3=True
  var4=[['x','y','z'],['x','c','d']]
  var5=r"C:\\Users\\me\\file.txt"
  
  myargs=var1,var2,var3,var4,var5
  my_lst_str = ' '.join(map(str, myargs))
  my_lst_str ='python analyzer.py '+my_lst_str
  
  subprocess.call(my_lst_str,shell=True)
  </code></pre>
  
  <p><strong>analyzer.py</strong></p>
  
  <pre><code>print 'Argument List:', str(sys.argv)
  </code></pre>
  
  <p>I have looked through similar questions on Stack Overflow. One oft-recommended solution I tried was importing analyzer.py as a module, but analyzer.py defines many different functions. Using it as a module creates lots of nested functions, and managing the scope of variables within those nested functions was cumbersome. </p>
  
  <p>I need to use Python 2 for these scripts. I'm on a Windows 10 machine.</p>