node与python交互,可以使用 python-shell 。
用法:
node中:
let { PythonShell } = require('python-shell')
let options = {
// mode: 'text',
// pythonPath: 'path/to/python', // 脚本目录
// pythonOptions: ['-u'], // get print results in real-time
// scriptPath: 'path/to/my/scripts',
args: [`value`] // 传参
}
// speesh.py 脚本名字
PythonShell.run('speesh.py', options, function (err, res) {
if (err) throw err;
console.log('finished', res); // finished [ "['speesh.py', 'value']"
]
});
python:
import sys
// 打印参数
print(sys.argv)
网友评论