运行 python 来自 Node.js (child_process) 的带有命名参数的脚本
Run python script from Node.js (child_process) with named arguments
我有一个 python 脚本,可以 运行 在命令行上使用此参数:
python2 arg1 --infile abc.csv --encrypt true --keyfile xyz.bin 1234 WOW path
但是,如果我尝试从 Node.js 子进程做同样的事情,我会得到一个错误:
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",
]);
不是运行宁并给出错误。
但是,如果我 运行 没有命名参数(--encrypt true)等,它 运行s 成功:
const process = spawn("python2", [
path.join(rootDir, "public", "python", "script.py"),
"arg1",
"1234",
"WOW",
"path",
]);
我认为我传递命名参数的方式可能不正确。
请帮忙!
这篇文章可能对您有用:
https://medium.com/swlh/run-python-script-from-node-js-and-send-data-to-browser-15677fcf199f
您需要拆分参数的每个部分:
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",
]);
您的原始脚本类似于运行在命令提示符下执行此操作:
python script.py arg1 "--infile abc.csv" "--encrypt true" "--keyfile xyz.bin" 1234 WOW path
基本上,您传递的参数名为 --infile abc.csv
,值为 --encrypt true
。这不是您想要的 运行。你想要的是:
python script.py arg1 --infile abc.csv --encrypt true --keyfile xyz.bin 1234 WOW path
我有一个 python 脚本,可以 运行 在命令行上使用此参数:
python2 arg1 --infile abc.csv --encrypt true --keyfile xyz.bin 1234 WOW path
但是,如果我尝试从 Node.js 子进程做同样的事情,我会得到一个错误:
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",
]);
不是运行宁并给出错误。 但是,如果我 运行 没有命名参数(--encrypt true)等,它 运行s 成功:
const process = spawn("python2", [
path.join(rootDir, "public", "python", "script.py"),
"arg1",
"1234",
"WOW",
"path",
]);
我认为我传递命名参数的方式可能不正确。 请帮忙!
这篇文章可能对您有用:
https://medium.com/swlh/run-python-script-from-node-js-and-send-data-to-browser-15677fcf199f
您需要拆分参数的每个部分:
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",
]);
您的原始脚本类似于运行在命令提示符下执行此操作:
python script.py arg1 "--infile abc.csv" "--encrypt true" "--keyfile xyz.bin" 1234 WOW path
基本上,您传递的参数名为 --infile abc.csv
,值为 --encrypt true
。这不是您想要的 运行。你想要的是:
python script.py arg1 --infile abc.csv --encrypt true --keyfile xyz.bin 1234 WOW path