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!
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