2

I have a python script which can be run with this argument on the command line:

python2 arg1 --infile abc.csv --encrypt true --keyfile xyz.bin 1234 WOW path

However, if I try to do the same thing from Node.js child process, I get an error:

const spawn = require("child_process").spawn;

const process = spawn("python2", [
  path.join(rootDir, "public", "python", "script.py"),
  "arg1",
  "--infile abc.csv",
  "--encrypt true",
  "--keyfile xyz.bin",
  "1234",
  "WOW",
  "path",
]);

It is not running and giving an error. But, If I run without the NAMED ARGUMENTS (--encrypt true) etc, it runs successfully:

const process = spawn("python2", [
  path.join(rootDir, "public", "python", "script.py"),
  "arg1",
  "1234",
  "WOW",
  "path",
]);

I think my way of passing the NAMED args might be incorrect. Please help!

2 Answers 2

3

You need to split each part of the argument:

const process = spawn("python2", [
  path.join(rootDir, "public", "python", "script.py"),
  "arg1",
  "--infile",
      "abc.csv", // indentation for clarity, it's not necessary
  "--encrypt",
      "true",
  "--keyfile",
      "xyz.bin",
  "1234",
  "WOW",
  "path",
]);

Your original script is similar to running this on the command prompt:

python script.py arg1 "--infile abc.csv" "--encrypt true" "--keyfile xyz.bin" 1234 WOW path

Basically you are passing the argument named --infile abc.csv with the value --encrypt true. Which is not what you intend to run. What you want is:

python script.py arg1 --infile abc.csv --encrypt true --keyfile xyz.bin 1234 WOW path
Sign up to request clarification or add additional context in comments.

Comments

0

You may find usefull this atricle:

https://medium.com/swlh/run-python-script-from-node-js-and-send-data-to-browser-15677fcf199f

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.