title: Call a Python function in a file with command-line argument
body: <p>I'm trying to pass arguments from Node.js to Python with <code>child_process</code> spawn. I also want to invoke the specific Python function with one of the argument that I specified in the Node.js array.</p>
  
  <p><code>test.js</code></p>
  
  <pre><code>'use strict';
  
  const path = require('path');
  const spawn = require('child_process').spawn;
  
  const exec = (file, fnCall, argv1, argv2) =&gt; {
    const py = spawn('python', [path.join(__dirname, file), fnCall, argv1, argv2]);
    py.stdout.on('data', (chunk) =&gt; {
      const textChunk = chunk.toString('utf8'); // buffer to string
      const array = textChunk.split(', ');
      console.log(array);
    });
  };
  exec('lib/test.py', 'test', 'argument1', 'argument2'.length - 2);  // =&gt; [ 'argument1', '7' ]
  exec('lib/test.py', 'test', 'arg3', 'arg4'.length - 2);  // =&gt; [ 'arg3', '2' ]
  </code></pre>
  
  <p>The second argument here is <code>test</code>, which should call the <code>test()</code> Python function.</p>
  
  <p><code>lib/test.py</code>:</p>
  
  <pre><code>import sys
  
  def test():
      first_arg = sys.argv[2]
      second_arg = sys.argv[3]
      data = first_arg + ", " + second_arg
      print(data, end="")
  
  sys.stdout.flush()
  </code></pre>
  
  <p>If I try to run this Python file without any Node.js from the command line, the execution looks like this:</p>
  
  <p><code>$ python lib/test.py test arg3 2</code></p>
  
  <p>Where <code>test</code>, <code>arg3</code>, and <code>2</code> are just command-line arguments, but <code>test</code> should call the <code>test()</code> function, which will use the <code>arg3</code>, <code>2</code> arguments for <code>print()</code>.</p>